javascript - Variable scope in dynamic functions? -
i'm new javascript , wrote following jquery code works:
function updateitems() { var = math.round((new date()).gettime() / 1000); $(".something").each(function() { $(this).html(now.tostring()); }); } updateitems();
why work? 1 might think now
not accessible inside function. guess run tests see happens if try modify now
inside function, if run each()
right after that, etc. basic understanding of how scope works here , in javascript cases appreciated.
also, type of function accurately called "dynamic function" or there better name it?
when use function() { ... }
, create technically called closure. can capture enclosing scope.
if use now
in closure, value of now
when execute closure, , (potentially) not value had when created it.
note enclosing scope here scope of outer function, not of outer block, , may have take care if you're creating closures in loop, instance.
Comments
Post a Comment