javascript - Is my understanding of closure wrong? -


i have read lot of example , question in stackoverflow closure,but feel i'm not able understand it, here get:

function testclosure (s1,s2){     return function(s3){         return s1 +' '+ s2 +' '+s3;     } }  var t1 = testclosure("test","test2");   console.log(t1("test3")); //test test2 test3 
  1. t1 holding function , scope chain of testclosure();
  2. t1 returning anonymous function itself.
  3. when t1 called puts inner function on testclosure(); accept last argument.
  4. s1,s2,s3 lookup through scope chain , return.

is understanding wrong?

  1. you execute testclosure.
  2. testclosure creates , returns new function. since function created inside of function, closure created , added new function's scope chain. closure has access arguments passed testclosure @ time of creation.
  3. variable t1 assigned returned function value.
  4. you call t1 , pass in argument. inside of t1 reference arguments part of closure.
  5. t1 cannot find values in current context, looks @ next scope in scope chain, closure.
  6. t1 finds values , finishes execution.

Comments

Popular posts from this blog

html5 - What is breaking my page when printing? -

html - Unable to style the color of bullets in a list -

c# - must be a non-abstract type with a public parameterless constructor in redis -