3

私のコードは次のとおりです。

(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();,誰が理由を教えてくれますか?

エラー情報は次のとおりです。

ここに画像の説明を入力

4

1 に答える 1

0

IEには関数式に関するいくつかの癖があります。考慮してください(現時点ではIE 8を持っていないため、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))
于 2012-09-07T05:55:32.697 に答える