window.onload = init();和有什么区别?和window.onload = init; [英] What is the difference between window.onload = init(); and window.onload = init;

查看:108
本文介绍了window.onload = init();和有什么区别?和window.onload = init;的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从我收集到的信息来看,前者将函数return语句的实际值分配给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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆