我的代码是:
(function(){
var test=function(){
if(this === window)
return new test();
}
test.prototype.play = function(){
alert("Hello");
};
window.Test=test;
})();
window.onload=function(){
Test().play();
};
这在 IE9+
firefox
chrome
中可以很好地工作,但是在 ie 6/7/8
中, Test().play();
中显示错误,谁能告诉我为什么?
错误信息为:
最佳答案
IE 对函数表达式有一些怪癖,请考虑(未在 IE 8 中测试,因为我目前没有它):
(function(global){
function test() {
if (this === global)
return new test();
}
test.prototype.play = function(){
alert("Hello");
};
global.Test = test;
})(this);
window.onload = function(){
Test().play();
};
另一种测试是:
if (!(this instanceof Test))
关于javascript - 为什么这个 === 窗口在 JavaScript 作用域安全构造函数中是假的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12311486/