为什么在JavaScript范围安全构造函数中此===窗口为false?

为什么在JavaScript范围安全构造函数中此===窗口为false?,javascript,constructor,window,this,Javascript,Constructor,Window,This,我的代码是: (function(){ var test=function(){ if(this === window) return new test(); } test.prototype.play = function(){ alert("Hello"); }; window.Test=test; })(); window.onload=function(){ Test().pla

我的代码是:

(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
中可以很好地工作,但在
IE6/7/8
中,
Test().play()中会显示一个错误,谁能告诉我为什么

错误信息为:


IE有一些与函数表达式有关的怪癖,考虑(在IE 8中没有测试,因为我现在没有):

另一种测试方法是:

    if (!(this instanceof Test))

如果该行导致错误,可能是因为
window.Test=Test未执行。您是否尝试过在
Test().play()周围添加try/catch块?您可能会在中找到答案。您会得到什么错误?虽然代码很难看,但并没有什么东西会马上出错。在IE7中对我来说似乎很好:@pst错误信息为“未定义”为null不是object,但什么时候将
窗口!==全局
?使用
此===全局
我得到相同的错误,如果(!(此测试实例))
正常,请使用
(我真的不知道[命名]函数表达式在这里是如何应用的。)@artwl好奇,使用
window.Test().play()时会发生什么?(使用
this===window
方法。)@pst'window.Test()为null或不是对象
    if (!(this instanceof Test))