简体   繁体   English

新手Javascript:使用添加的括号调用函数时不运行(即使它不需要参数)

[英]Newbie Javascript: Function doesn't run when calling it with added parenthesis (even though it doesn't need parameters)

Here's my function to alert me of the number of childNodes of the body tag. 这是我的功能,提醒我body标签的childNodes数量。

    function countBodyChildren() {
      var body_element = document.getElementsByTagName("body")[0];
      alert(body_element.childNodes.length);
}

 window.onload = countBodyChildren;

The function runs fine, but when I add () to the end of the function name ie window.onload = countBodyChildren(); 该函数运行正常,但是当我将add ()添加到函数名的末尾时,即window.onload = countBodyChildren(); the function does not run. 该功能无法运行。 Why is this? 为什么是这样? I thought to call functions, even if they don't need any parameters, you always add the () to the end of its name. 我想调用函数,即使它们不需要任何参数,你总是将()添加到它的名字的末尾。

I thought to call functions, even if they don't need any parameters, you always add the () to the end of its name. 我想调用函数,即使它们不需要任何参数,你总是将()添加到它的名字的末尾。

You thought correctly. 你没想到。 But here, you don't want to call the function; 但是在这里,你不想调用这个函数; you want to tell the browser which function to call when the time comes. 你想告诉浏览器什么时候来电话。

When you add the parentheses, you will call the function, immediately (not on load). 添加括号时,将立即调用该函数(不加载)。 The function executes; 该函数执行; tries to look up the body elements, but there is none loaded yet; 试图查找body元素,但尚未加载; so you body_element ends up as undefined . 所以你body_element最终undefinedundefined You look up childNodes on undefined , and get an error. 您在undefined查找childNodes ,并收到错误。 window.onload assignment does not happen. window.onload赋值不会发生。

Even if your function did not throw an error, it would have returned undefined , so window.onload becomes undefined , so nothing happens when the page is finally loaded. 即使你的函数没有抛出错误,它也会返回undefined ,因此window.onload变得undefined ,所以当最终加载页面时没有任何反应。

Instead, you need to start thinking that functions are just another kind of value. 相反,你需要开始认为函数只是另一种价值。 When you say 当你说

function a() {
  // ...
}

it is the same as this: 它与此相同:

a = function() {
  // ...
};

(Actually there are differences, important ones, but they're irrelevant for now; look them up when you have the basics down pat). (实际上存在差异,重要的是,但它们现在无关紧要;当你掌握了基础知识时,请查看它们)。 A function is now "contained" in variable a . 现在,函数“包含”在变量a Then you can do this: 然后你可以这样做:

var b = a;
b();

and it will work - you're putting whatever was in a (your function) into the variable b , and then executing it by invoking its new name (since now both a and b refer to the same function). 它会工作 - 你把(你的函数)中a任何东西放入变量b ,然后通过调用它的新名称来执行它(因为现在ab引用相同的函数)。 In the same way, after 以同样的方式,之后

window.onload = countBodyChildren;

the onload property of the window object now contains your function. window对象的onload属性现在包含您的函数。 Browser will then execute whatever is there when the load finishes. 然后,浏览器将执行加载完成后的任何操作。

You had a case of premature execution. 你有一个过早执行的情况。

What window.onload expects is a reference to a function or a function definition. window.onload期望的是对函数或函数定义的引用。

When you put parenthesis, you actually execute the function and it is the return value of the function that is assigned to window.onload . 放置括号时,实际执行该函数,它是分配给window.onload的函数的返回值。

window.onload = countBodyChildren(); window.onload = countBodyChildren();

That says "call the function, and assign the return value of the function to the onload property of the window object. Your function returns nothing. 这就是说“调用函数,并将函数的返回值赋给window对象的onload属性。你的函数什么都不返回。

For example 例如

function countBodyChildren() {
      var body_element = document.getElementsByTagName("body")[0];
      alert(body_element.childNodes.length);
return 5;
}
window.onload = countBodyChildren();

that would assign 5 to the onload prop. 这将为onload道具分配5。 Not what you want. 不是你想要的。 you need to assign a function reference to the onload prop, so the js engine can call the function when the event occurs. 你需要为onload prop分配一个函数引用,所以js引擎可以在事件发生时调用该函数。

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

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