闭包函数的理解

 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();


第一步:将fn赋值给s,console.log(s)输出的是返回值。

第二步:因为返回值是个函数s,将s()赋值给是s1,var s1=s(),相当于运行了s(),输出hello,且将s赋值给了s1,
console.log(s1)输出的是一个返回函数s1;

第三步:s1();执行了s1,输出hello
 

猜你喜欢

转载自www.cnblogs.com/hy96/p/11372429.html