javascript - Calling private function from inside another function -
i'm having trouble getting head around calling internal function inside another. need able call funca on page load passing element , dimensions applies styles passes elem. funcb uses said parameters size element correctly:
var funca = function(elem, width, height) { //performs layout restyle function funcb() { //performs sizing } funcb(); }
however, issue need recall funcb within debounced resize function so.
function debounce(func, wait, immediate) { var timeout; return function() { var context = this, args = arguments; var later = function() { timeout = null; if (!immediate) func.apply(context, args); }; var callnow = immediate && !timeout; cleartimeout(timeout); timeout = settimeout(later, wait); if (callnow) func.apply(context, args); }; }; var resizefn = debounce(function() { funcb(); }, 10); $(window).on('resize', resizefn);
what's best practice making funcb available? i'd been considering returning , caching variable:
var funca = function(elem, width, height) { //performs layout restyle function funcb() { //performs sizing } funcb(); return funcb } var scopedfuncb = funca; scopedfuncb();
but there better way?
i'd been considering returning it
yes, that's best practise. caller can decide , when , how call it.
…and caching variable
no need actually. can directly pass debounce
without further ado:
$(window).on('resize', debounce(funca(elem, width, height), 10));
Comments
Post a Comment