代码1function tt(){ if(typeof a=="undefined"){
a=new Object();
} return a;
}这里的a=new Object();相当于是window.a=new Object();了。代码2function tt(){ return function(){ if(typeof a=="undefined"){
a=new Object();
} return a;
}
}这里的a=new Object();就完全不是window.a=new Object();了。证明是window.a值为undefined。请问为什么会这样另外问题还有这样的问题在严格模式下 变量的声明都需要var 不然会报错。以同样的代码 的代码为例代码1.1function tt(){ "use strict"; if(typeof a=="undefined"){
a=new Object();
} return a;
}报错。这个很符合期望。代码2.1function tt(){"use strict"; return function(){ if(typeof a=="undefined"){
a=new Object();
} return a;
}
}这个没有报错,也感到奇怪。这和严格模式下定义变量必须使用var不符啊
1 回答

慕勒3428872
TA贡献1848条经验 获得超6个赞
我确定这是你的问题,我肯定你没有尝试过下面这样的代码
function tt(){ return function(){ if(typeof a=="undefined"){ a=new Object(); } return a; } }var fn = tt();fn();// 或者 tt()();console.log(a);
对于第二个
function tt(){ “use strict” return function(){ if(typeof a=="undefined"){ a=new Object(); } return a; } } tt()();
添加回答
举报
0/150
提交
取消