有什么区别
while(condition){
var variable;
...
}
Run Code Online (Sandbox Code Playgroud)
和
while(condition){(function(){
var variable;
...
})();}
Run Code Online (Sandbox Code Playgroud)
有人可以解释我的干涉吗?
在第一种情况下,变量可以在while(甚至在它之后)的任何地方访问.在第二种情况下,它是私有的,只能在自动调用匿名函数中访问.所以差异基本上在变量的范围内.第二个例子看起来很复杂而没有提供更多的上下文.
第一个例子:
while(condition) {
var variable;
... // the variable is accessible here
}
// the variable is accessible here
Run Code Online (Sandbox Code Playgroud)
第二个例子:
while(condition) {
(function() {
var variable;
... // the variable is accessible here
})();
// the variable is NOT accessible here
}
// the variable is NOT accessible here
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
202 次 |
| 最近记录: |