javascript - Creating closures in loops -


this question has answer here:

i learning closures , have basics on , how work.

i got following code mdn , know solution since it's in same article. don't how possible:

<p id="help">helpful notes appear here</p> <p>e-mail: <input type="text" id="email" name="email"></p> <p>name: <input type="text" id="name" name="name"></p> <p>age: <input type="text" id="age" name="age"></p>   function showhelp(help) {   document.getelementbyid('help').innerhtml = help; }   function setuphelp() {   var helptext = [       {'id': 'email', 'help': 'your e-mail address'},       {'id': 'name', 'help': 'your full name'},       {'id': 'age', 'help': 'your age (you must on 16)'}     ];    (var = 0; < helptext.length; i++) {     var item = helptext[i];     document.getelementbyid(item.id).onfocus = function() {       showhelp(item.help);     }   } }  setuphelp(); 

i know section of code needs change work is:

document.getelementbyid(item.id).onfocus = function() {        showhelp(item.help);      } 

how @ end of loop text being pointed is: your age (you must on 16) elements?

i can follow code , see loop loops through elements correctly can't head around how last item pointed elements @ end your age ... since saves each 1 individually onfocus = funtion()... , ever item.help @ time passed in , saved.

any step step explanation in me understanding going on.

javascript has functional scope. means variables defined in function hoisted top of function. c# or java has block level scope defining item in loop natural way in languages . not case in javascript. avoid confusion variables should declared @ top of function. js interpreter hoist var item top of function when evaluates it. in loop setting onfocus function variable referencing item in the parent function scope of setuphelp. @ end of loop, item has assigned value of last indexed item. every time onfocus executes function references assigned item value in setuphelp function scope. in order create closure need execute anonymous function in each iteration of loop create new functional scope. new scope should have var assigned current item outer function.

this example shows functional scope closure in each iteration of for loop:

function showhelp(help) {   document.getelementbyid('help').innerhtml = help; }   function setuphelp() {   var helptext = [       {'id': 'email', 'help': 'your e-mail address'},       {'id': 'name', 'help': 'your full name'},       {'id': 'age', 'help': 'your age (you must on 16)'}     ];    (var = 0; < helptext.length; i++) {     var item = helptext[i];     document.getelementbyid(item.id).onfocus = (function() {         var saveitem = item;         return function () {             showhelp(saveitem.help);         }     })();   } }  setuphelp(); 

example fiddle:

https://jsfiddle.net/ohoy75kh/1/

or

https://jsfiddle.net/ohoy75kh/2/


Comments

Popular posts from this blog

OpenCV OpenCL: Convert Mat to Bitmap in JNI Layer for Android -

android - org.xmlpull.v1.XmlPullParserException: expected: START_TAG {http://schemas.xmlsoap.org/soap/envelope/}Envelope -

python - How to remove the Xframe Options header in django? -