前两天看到一道笔试题。。总感觉他的语法有问题。后来发现完全没有问题。完全是自己对JS自执行函数的其他写法不了解。才会有这样的误解
写法一
(function(a,b){
console.log(a+b)
})(1,2)
//3
写法2
(function(a,b){
console.log(a+b)
}(1,2))
//3
分析一下下面的代码
var test = (function(a){
this.a =a;
return function(b){
return this.a+b;
}
}(function(a,b){
return a//1
}(1,2)))
console.log(test(4))
其实完全可以这样理解
var test = (function(a){
this.a =a;
return function(b){
return this.a+b;
}
})(function(a,b){
return a//1
}(1,2))
console.log(test(4))
//这样瞬间就能知道输出的是5了










网友评论