简体   繁体   English

window.onload = someFunctionName与window.onload = someFunctionName()

[英]window.onload = someFunctionName vs window.onload = someFunctionName()

So the question is what is the difference between 所以问题是

window.onload = someFunctionName vs. window.onload = someFunctionName() and should I use this parentheses? window.onload = someFunctionNamewindow.onload = someFunctionName() ,我应该使用此括号吗?

Here is an excellent specification: https://developer.mozilla.org/en-US/docs/Web/API/GlobalEventHandlers.onload 这是一个很好的规范: https : //developer.mozilla.org/en-US/docs/Web/API/GlobalEventHandlers.onload

There are several ways to set it: 1. Javascript, which requires setting it to a function. 有几种设置方法:1. Javascript,需要将其设置为函数。 2. Setting it via an HTML tag as a string value. 2.通过HTML标记将其设置为字符串值。

Examples of both here and are valid ways. 这两个示例都是有效的方法。 I would recommend Javascript directly passing a function reference: http://www.w3schools.com/jsref/event_onload.asp 我建议Javascript直接传递函数引用: http : //www.w3schools.com/jsref/event_onload.asp

window.onload = someFunctionName(); will call the someFunctionName function and assign its return value to the onload property. 调用 someFunctionName函数并将其返回值分配给 onload属性。

window.onload = someFunctionName; assigns the someFunctionName function to the property onload . someFunctionName函数分配给属性onload The browser will call that function when the load event occurs. 发生load事件时,浏览器将调用该函数。 Functions are objects. 功能是对象。 When you refer to them by their name without calling them, you're referring to the object. 当您通过它们的名称引用它们而不调用它们时,就是在引用对象。

Example: 例:

window.onload = test();

function test() {
    alert("outer");
    return function(){
    alert("inner");
    }
}

this will alert twice as test() is called it will alert outer and return innerfunction to load and onload it will alert inner. 这将发出两次警报,因为调用了test()它将警报外部,并返回innerfunction以加载和加载它将警报内部。

while

window.onload = test;

function test() {
    alert("outer");
    return function(){
    alert("inner");
    }
}

above code will only alert outer because innerfunction returned but never get called. 上面的代码只会警告外部,因为内部函数已返回但从未被调用。

Here is fiddle 这是小提琴

window.onload = someFunction; - assigns someFunction to onload event -将someFunction分配给onload事件

window.onload = someFunction(); - assigns a RESULT of someFunction to onload event. -将someFunction的RESULT分配给onload事件。

You need to be aware that in Javascript a function can return another function after execution . 您需要注意, 在Javascript中,一个函数在执行后可以返回另一个函数 For this reason, because you are not showing any code, both examples might be correct. 由于这个原因,因为您没有显示任何代码,所以两个示例都可能是正确的。

But there are differences between the two: 但是两者之间有区别:

  • window.onload = someFunctionName assigns the someFunctionName itself as the onload handler; window.onload = someFunctionName分配someFunctionName本身作为onload处理;

  • window.onload = someFunctionName() does not assign the function someFunctionName itself but the returning result of the execution of the someFunctionName function. window.onload = someFunctionName()不会为函数分配someFunctionName本身,而是为someFunctionName函数的执行返回结果。 Assuming someFunctionName itself returns a function then it might be correct, otherwise you will get unexpected results. 假设someFunctionName本身返回一个函数,那么它可能是正确的,否则您将得到意外的结果。

Javascript functions are objects. Javascript函数是对象。 When you use parentheses you are invoking the function. 使用括号时,您正在调用该函数。 When you use the function without parentheses you are referring to the function object (ie the function itself). 当使用不带括号的函数时,是指函数对象(即函数本身)。

So it depends on what you are doing... 所以这取决于你在做什么...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM