Appreciated closure function

 function fn() {
return function () { //s;
console.log("hello")
return function () { //s1
console.log("world")
}
}
}
var s=fn()
console.log(s);
var s1=s()
console.log(s1)
s1();


The first step: fn assigned to s, console.log (s) output is the return value.

Step: Because the return value is a function of s, the s () is assigned to s1, var s1 = s () , equivalent to the operation s (), the output hello, and the s is assigned to the S1,
the console.log ( s1) is a function that returns output S1;

step: s1 (); performed s1, the output hello
 

Guess you like

Origin www.cnblogs.com/hy96/p/11372429.html