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
- t1 holding function , scope chain of
testclosure();
- t1 returning anonymous function itself.
- when t1 called puts inner function on
testclosure();
accept last argument. - s1,s2,s3 lookup through scope chain , return.
is understanding wrong?
- you execute
testclosure
. testclosure
creates , returns new function. since function created inside of function, closure created , added new function's scope chain. closure has access arguments passedtestclosure
@ time of creation.- variable
t1
assigned returned function value. - you call
t1
, pass in argument. inside oft1
reference arguments part of closure. t1
cannot find values in current context, looks @ next scope in scope chain, closure.t1
finds values , finishes execution.
Comments
Post a Comment