[英]Why can't I assign undefined to window.load?
我只是注意到這種奇怪的效果
window.onload = undefined;
console.log(window.onload); // print 'null', instead of 'undefined'
雖然它可以對其他對象(包括內置對象)正常工作,例如
Array.prototype.slice = undefined;
console.log(Array.prototype.slice); // print 'undefined'
為什么會這樣?
之所以.onload
是因為.onload
是一個setter,它的工作原理如下:
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
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.