2

私はちょうどこの奇妙な効果に気づいた

window.onload = undefined;
console.log(window.onload); // print 'null', instead of 'undefined'

組み込みのオブジェクトを含む他のオブジェクトでは期待どおりに機能しますが、たとえば

Array.prototype.slice = undefined;
console.log(Array.prototype.slice); // print 'undefined'

なぜそうなのですか?

4

1 に答える 1

4

.onloadはセッターであり、次のように機能するため、この動作は次のようになります。

window = {
    // Other window properties and methods

    get onload() {
        // returns null if no function was added or returns the last function added
    },

    set onload(value) {
        if (typeof value === 'function') {
            loadListener = value; // loadListener is the function called when load event is triggered
        }
    }

    // Other window properties and methods
}
于 2012-08-11T08:51:56.573 に答える