javascript - Nested function: am I creating 10 functions here? -
i'm working on function complicated (processor) embedded part of functionality in nested "utility function" inside (print). simplified version of looks this:
var out = document.getelementbyid( "output" ); function processor ( n ) { function print( msg ) { out.innerhtml += msg; } while ( n > 0 ) { print( n-- ); print( ", " ); } print( "<br/>" ); } ( var = 0; < 10; i++ ) { processor( ); } you can see in action in this jsfiddle.
the question is: creating 10 instances of utility function print()? if yes, more efficient way write code without putting utility function outside processor() function? want print() function accessible in processor() , else. 1 solution namespace.
i've read question , though it's related it's not directly answer: efficiency of creating event handler in nested loop: creating 1440 functions here?
in short, yes are. since function local, they'll popped out of stack , destroyed when function returns. since calling processor() function serially in loop, once instance of print() function valid , accessible @ point of time.
Comments
Post a Comment