[英]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.