JavaScript的! window.onload = someFunction和window.onload = someFunction() [英] JavaScript! window.onload = someFunction and window.onload = someFunction()

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

问题描述

之间是否存在差异:


  1. window.onload = someFunction;

window.onload = someFunction();

最后的括号。他们有什么不同吗?

The parentheses at the end. Do they make any difference?

我们通常使用第一个!如果我们必须将一些参数传递给函数,该怎么办?我们如何使用第一个语句来做?

We generally use the first one! What if we Have to pass some parameter to the function. How will we do it by using the first statement?

推荐答案

如另有说明,第一个表格

As explained otherwise, the first form

window.onload = someFunction 

只需将onload变量设置为等于someFunction函数;当页面完成加载时,将调用此函数。

Simply set the "onload" variable to be equals to the "someFunction" function ; when the page finishes loading, this function is called.

另一种形式:

window.onload = someFunction()

将onload变量设置为调用someFunction的 result 。除非someFunction本身返回一个函数,否则这可能不是你想要做的。

Sets the "onload" variable to be the result of calling someFunction. Unless "someFunction" itself returns a function, this is probably not what you want to do.

默认情况下,使用单个event参数调用onload函数。如果你想传递参数,你可能会做这样的事情:

By default, the onload function is called with a single "event" argument. If you want to pass arguments, you might be able to do something like this :

window.onload = function (event) {
  someFunction(someArg, someOtherArg)
}

这篇关于JavaScript的! window.onload = someFunction和window.onload = someFunction()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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