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

Popular posts from this blog

c# - How Configure Devart dotConnect for SQLite Code First? -

java - Copying object fields -

c++ - Clear the memory after returning a vector in a function -