调用函数和引用函数之间的区别

调用函数和引用函数之间的区别

本文介绍了调用函数和引用函数之间的区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

查看以下代码:

window.onload = someFunction;

很多时候我看到使用这种代码甚至我都使用相同的代码。但是,仍有一些事情尚不清楚。

Many times I see the use of this kind of code and even I use the same. But, there are still few things unclear.


  1. 上面是一旦页面加载就调用 someFunction 。但是,我们不应该用括号()来调用函数吗?

  2. 根据我的理解,我们只是分配 someFunction window.onload ,因此我们可以或不应该调用 window.onload() ;

  3. 是否有这种情况,当 functionName() functionName becames可互换。因为在很多地方和各种API,我看到调用函数没有括号。

  1. The above is a call to someFunction as soon as the page loads, right. But, aren't we supposed to call a function with brackets ()?
  2. From my understanding, we are just assigning someFunction to window.onload, therefore can we or shouldn't we call window.onload();
  3. Are there such cases, when functionName() and functionName becames interchangeable. Because at many places, and in various API, I have seen calling the function without brackets.


推荐答案

你不是调用这个函数,你告诉浏览器 调用哪个函数,onload。

You're not calling the function, you're telling the browser which function to call, onload.

引用函数并调用函数永远不可互换:它们完全不同。

Referencing a function and calling a function are never interchangeable: they're completely different things.

但是,您可以调用返回的函数 a function:

You may, however, call a function that returns a function:

function getFunction() {
    return function() {
        alert("I'm the real onload function.");
    };
}

window.onload = getFunction();

这篇关于调用函数和引用函数之间的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-24 20:58