JavaScript!window.onload = someFunction 和 window.onload = someFunction()

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15526544/
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-10-27 01:06:10  来源:igfitidea点击:

JavaScript! window.onload = someFunction and window.onload = someFunction()

javascript

提问by Navneet Saini

Is there a difference between:

是否有区别:

  1. window.onload = someFunction;

  2. window.onload = someFunction();

  1. window.onload = someFunction;

  2. 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?

我们一般用第一个!如果我们必须向函数传递一些参数怎么办。我们将如何使用第一个语句来做到这一点?

回答by phtrivier

As explained otherwise, the first form

如前所述,第一种形式

window.onload = someFunction 

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

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

The other form :

另一种形式:

window.onload = someFunction()

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

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

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 :

默认情况下,使用单个“事件”参数调用 onload 函数。如果你想传递参数,你可以这样做:

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

回答by MMM

Your second statement assigns the resultof someFunction()to window.onload.

你的第二个语句将结果someFunction()window.onload

If you want to add a parameter, you can do the following:

如果要添加参数,可以执行以下操作:

window.onload = function () {
    someFunction(parameter);
};

回答by Quentin

window.onload = someFunction;assigns a function to onload.

window.onload = someFunction;为 分配一个函数onload

window.onload = someFunction();callsa function and assigns its return value to onload. (This is not desirable unless the return value of that function is another function).

window.onload = someFunction();调用一个函数并将其返回值赋给onload. (除非该函数的返回值是另一个函数,否则这是不可取的)。

What if we Have to pass some parameter to the function

如果我们必须向函数传递一些参数怎么办

Usually you define a new function which does nothing except call the original function with some arguments, then you assign the new function to the event handler.

通常,您定义一个新函数,除了使用一些参数调用原始函数外,什么都不做,然后将新函数分配给事件处理程序。

回答by Jakub Michálek

if you use

如果你使用

window.onload = someFunction;

a function with the name of someFunction is called on window.onload

在 window.onload 上调用了一个名为 someFunction 的函数

if you use

如果你使用

window.onload = someFunction();

someFunction() runs and the result is assigned to window.onload

someFunction() 运行并将结果分配给 window.onload

回答by Jeremy Blalock

Yes. If you put window.onload = someFunction()it is expected that the result of someFunction()is another function. This could be used for wrapping a function, or passing parameters. For instance:

是的。如果你把window.onload = someFunction()它预期的结果someFunction()是另一个函数。这可用于包装函数或传递参数。例如:

window.onload = myFunction(arg1, arg2)

myFunction(arg1, arg2)would be expected to return some function incorporating these two variables.

myFunction(arg1, arg2)预计会返回一些包含这两个变量的函数。