Javascript window.onload = init(); 有什么区别?和 window.onload = init;

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8830074/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 07:42:27  来源:igfitidea点击:

What is the difference between window.onload = init(); and window.onload = init;

javascriptscripting

提问by Andy

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.

根据我收集的信息,前者将函数 return 语句的实际值分配给 onload 属性,而后者分配实际函数,并将在窗口加载后运行。但我仍然不确定。感谢任何可以详细说明的人。

回答by Adam Rackis

window.onload = init();

assigns the onload event to whatever is returnedfrom the init function when it's executed. initwill be executed immediately, (like, now, notwhen 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:

将 onload 事件分配给执行时从 init 函数返回的任何内容。 init将执行立即,(比如,现在不是当窗口完成装载),结果将被分配到window.onload。您不太可能想要这个,但以下内容是有效的:

function init() {
   var world = "World!";
   return function () {
      alert("Hello " + world);
   };
}

window.onload = init();


window.onload = init;

assigns the onload event to the function init. When the onload event fires, the init function will be run.

将 onload 事件分配给函数 init。当 onload 事件触发时,将运行 init 函数。

function init() {
   var world = "World!";
   alert("Hello " + world);
}

window.onload = init;

回答by RobG

window.onload = foo;

assigns the value of footo the onload property of the window object.

foo的值赋给window 对象的 onload 属性。

window.onload = foo();

assigns the value returned by calling foo()to the onload property of the window object. Whether that value is from a return statement or not depends on foo, but it would make sense for it to return a function (which requires a return statement).

将调用foo()返回的值赋给window 对象的 onload 属性。该值是否来自 return 语句取决于foo,但它返回一个函数(需要 return 语句)是有意义的。

When the load event occurs, if the value of window.onloadis a function reference, then window's event handler will call it.

当 load 事件发生时,如果window.onload的值是一个函数引用,那么 window 的事件处理程序就会调用它。

回答by Arman McHitarian

Good answers, one more thing to add:

很好的答案,还有一件事要补充:

Browser runtimes ignore non-object (string, number, true, false, undefined, null, NaN) values set to the DOM events such as window.onload. So if you write window.onload= 10 or any of the above mentioned value-types (including the hybrid string) the event will remain null.

浏览器运行时会忽略string, number, true, false, undefined, null, NaN设置为 DOM 事件(例如 window.onload)的非对象 ( ) 值。因此,如果您编写window.onload= 10 或上述任何值类型(包括混合string),则事件将保持为null

What is more funny that the event handlers will get any object type values, even window.onload = new Dateis a pretty valid code that will prompt the current date when you log the window.onload. :) But sure nothing will happen when the window.loadevent fires.

更有趣的是,事件处理程序将获取任何对象类型值,甚至window.onload = new Date是一个非常有效的代码,它会在您登录window.onload. :) 但是当window.load事件触发时肯定什么都不会发生。

So, always assign a function to any event in JavaScript.

因此,始终为 JavaScript 中的任何事件分配一个函数。