Matej Posted July 20, 2014 Share Posted July 20, 2014 Hi , what is(are) function enviroment and scope? This is the first time i hear something like that . Im asking because of this example http://jsfiddle.net/v7gjv/1/ and the text that explains why is there makeHelpCallback closure. "The reason for this is that the functions assigned to onfocus are closures; they consist of the function definition and the captured environment from the setupHelp function's scope. Three closures have been created, but each one shares the same single environment. By the time the onfocus callbacks are executed, the loop has run its course and the item variable (shared by all three closures) has been left pointing to the last entry in the helpText list." Thanks for answers Link to comment Share on other sites More sharing options...
davej Posted July 21, 2014 Share Posted July 21, 2014 I don't particularly like that jsfiddle example but in Javascript the code inside a function can access the local variables of that function and also all of the variables of all parent functions -- even if the parent function has terminated. This essentially creates static local variables that are private to the function and is known as a "closure." It generally occurs when there is a function inside a function and the inner function is set up as a event handler. <!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><title>Example Closure</title><script>window.onerror = function(m, u, l){alert('Javascript Error: '+m+'nURL: '+u+'nLine Number: '+l);return true;}</script><script>function init() {var lasttime = null; //this variable is a static local function dtime(){ var now = new Date().getTime(); var delta; if (lasttime == null){ delta = 0; }else{ delta = now - lasttime; } lasttime = now;// update the static local var document.getElementById("out").innerHTML = delta + ' milliseconds'; }document.getElementById('btn1').onclick = dtime;}//end of initwindow.addEventListener('load',init, false);</script></head><body><p>Example Closure</p><p id="out"></p><button id="btn1">time between clicks</button></body></html> Link to comment Share on other sites More sharing options...
Matej Posted July 21, 2014 Author Share Posted July 21, 2014 Thanks , but this function seems wierd to me function init() {var lasttime = null; //this variable is a static localfunction dtime(){var now = new Date().getTime();var delta;if (lasttime == null){ // this is true so delta will be 0 on the first invoke , delta = 0;}else{delta = now - lasttime; //on the second and so on invokes this is true , but shouldnt this equals to 0 also? basicly now=new Date().getTime() and "lasttime"=now , so it should equals 0 shouldnt it?}lasttime = now;// update the static local vardocument.getElementById("out").innerHTML = delta + ' milliseconds';}document.getElementById('btn1').onclick = dtime;}//end of init window.addEventListener('load',init, false); Link to comment Share on other sites More sharing options...
Matej Posted July 21, 2014 Author Share Posted July 21, 2014 oh , basicly lastime isnt updated , silly me Link to comment Share on other sites More sharing options...
davej Posted July 21, 2014 Share Posted July 21, 2014 lasttime is set to null when the init() function runs, When the button is clicked the inner function dtime() runs and ends with an update... lasttime = now;// update the static local var Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now