window.onload = init(); 有什么区别?和 window.onload = init; [英] What is the difference between window.onload = init(); and window.onload = init;
问题描述
从我收集到的信息来看,前者将函数返回语句的实际值分配给 onload 属性,而后者分配实际函数,并将在窗口加载后运行.但我仍然不确定.感谢任何可以详细说明的人.
From what I have gathered, the former assigns the actual value of whatever that functions return statement would be to the onload property, while the latter assigns the actual function, and will run after the window has loaded. But I am still not sure. Thanks for anyone that can elaborate.
推荐答案
window.onload = init();
将 onload 事件分配给执行时从 init 函数返回的任何内容.init
将立即执行,(例如,现在,不在窗口加载完成后)和结果将分配给 window.onload
.您不太可能想要这个,但以下内容是有效的:
assigns the onload event to whatever is returned from the init function when it's executed. init
will be executed immediately, (like, now, not when the window is done loading) and the result will be assigned to window.onload
. It's unlikely you'd ever want this, but the following would be valid:
function init() {
var world = "World!";
return function () {
alert("Hello " + world);
};
}
window.onload = init();
<小时>
window.onload = init;
将 onload 事件分配给函数 init.当 onload 事件触发时,将运行 init 函数.
assigns the onload event to the function init. When the onload event fires, the init function will be run.
function init() {
var world = "World!";
alert("Hello " + world);
}
window.onload = init;
这篇关于window.onload = init(); 有什么区别?和 window.onload = init;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!