why the function call does not work this way?
This is the script that i tested when the page loads.
window.onload = initAll;
function initAll() {
document.getElementById("pTag").innerHTML = "Hello Java Script !";
}
The above script works fine till i put parenthesis like initAll()
during the call. _( window.onload=initAll(); )_
. Nothing happens if use initA开发者_C百科ll()
during function call. It is like the script wasn't there.
Why is it so ?
window.onload
expects the value you set for it to be a function (functions are like any other object in JavaScript).
If you put parens after initAll
, you actually invoke the function and set window.onload
to the function's return value. Since the function does not return
anything explicitly, this value is undefined
. So in practice you can think of this code:
window.onload = initAll();
function initAll() {
// do something with document
}
as equivalent to this code:
var result = initAll();
window.onload = result;
function initAll() {
return null;
}
I 'm sure you can immediately see why this would not work.
Because you should not execute the function at that line, you should assign an event handler to the window object, which will be triggered/executed once the load
event has fired, not the returned value of a function.
However, assigning an event handler window.onload
is not recommended, because it will allow for only one handler to be attached. Consider using addEventListener
.
Because you're not calling the function directly in the window.onload statement, but you're assigning a function to the onload event. The system will then automatically call your initall function when the event happens. Thats why you need the function itself (without parenthesis), and not the call (with parenthesis)
This has to do with the way Javascript works. Functions also are actually objects, like about everything in javascript.
So basically, you are assigning the "Object" initAll
, which happens to be a function, to window.onload
. This object is then called when the onload event is triggered, assuming it is a function.
Now, when you are putting parenthesis behind your objects name, you are basically telling it to treat that object like a function and call it. this is not the way how to assign it to a property, like the window.onload
property.
window.onload = initAll;
You're binding the initAll
function to the onload
event of the window
object. The function becomes the load handler. Now, when the load
event fires at the window
object, this function will be invoked.
window.onload = initAll();
You're immediately invoking the function initAll
. this invocation returns undefined
(in your case), and that undefined
value will be assigned to window.onload
. Assigning the undefined
value to onevent
properties obviously has no effect.
When you omit the parentheses in this call:
window.onload = initAll;
You are assigning to the window.onload event the reference value of the function initAll()
. This causes the function to execute when the onload event is called because they share the same reference point. When you use the assignment like this:
window.onload = initAll();
You are assigning to the window.onload event the returned value of the function, which in this case is null.
精彩评论