函数调用中"()"的含义是什么?

Isa*_*bow 19 javascript

现在,我通常调用一个函数(不需要参数),()如下所示:

myFunction(); //there's empty parens
Run Code Online (Sandbox Code Playgroud)

除了jQuery调用,我可以逃脱:

$('#foo').bind('click', myFunction); //no parens
Run Code Online (Sandbox Code Playgroud)

精细.但最近我看到这个评论这里的SO:

"考虑使用setTimeout(monitor, 100);而不是setTimeout('monitor()', 100);.Eval是邪恶的:)"

哎呀!我们真的eval()在这里做一个字符串吗?我想我并不真正理解"调用"函数的意义和含义.有关调用和引用函数的真正规则是什么?

Anu*_*rag 44

在JavaScript中,函数是第一类对象.这意味着您可以将函数作为参数传递给函数,或者将它们视为变量.

假设我们正在谈论一个函数hello,

function hello() {
    alert('yo');
}
Run Code Online (Sandbox Code Playgroud)

当我们简单地写

hello
Run Code Online (Sandbox Code Playgroud)

我们指的是不执行它的内容的函数.但是当我们()在函数名后添加parens 后,

hello()
Run Code Online (Sandbox Code Playgroud)

然后我们实际上调用了将在屏幕上提醒"yo"的功能.

bindjQuery中的方法接受事件类型(字符串)和函数作为其参数.在您的示例中,您将传递类型 - "单击"和实际函数作为参数.

Have you seen Inception? Consider this contrived example which might make things clearer. Since functions are first-class objects in JavaScript, we can pass and return a function from within a function. So let's create a function that returns a function when invoked, and the returned function also returns another function when invoked.

function reality() {
    return function() {
        return function() {
            alert('in a Limbo');
        }
    };
}
Run Code Online (Sandbox Code Playgroud)

Here reality is a function, reality() is a function, and reality()() is a function as well. However reality()()() is not a function, but simply undefined as we are not returning a function (we aren't returning anything) from the innermost function.

So for the reality function example, you could have passed any of the following to jQuery's bind.

$('#foo').bind('click', reality);
$('#foo').bind('click', reality());
$('#foo').bind('click', reality()());
Run Code Online (Sandbox Code Playgroud)

  • 启动参考+1(当然还有很好的解释) (3认同)
  • 是的,我现在保留了所有SO答案中使用"功能"最多次的记录 - 27. (2认同)
  • 怎么样`var f = function(){return f; 作为一个例子?允许你键入`f()()()()()()()()()()()()()()()`_ad infinitum_,这是非常酷的. (2认同)

CMS*_*CMS 7

Your jQuery bind example is similar to setTimeout(monitor, 100);, you are passing a reference of a function object as an argument.

Passing a string to the setTimeout/setInterval methods should be avoided for the same reasons you should avoid eval and the Function constructor when it is unnecessary.

The code passed as a string will be evaluated and run in the global execution context, which can give you "scope issues", consider the following example:

// a global function
var f = function () {
  alert('global');
};

(function () {
  // a local function
  var f = function() {
    alert('local');
  };

  setTimeout('f()', 100); // will alert "global"
  setTimeout(f, 100);     // will alert "local"
})();
Run Code Online (Sandbox Code Playgroud)

The first setTimeout call in the above example, will execute the global f function, because the evaluated code has no access to the local lexical scope of the anonymous function.

If you pass the reference of a function object to the setTimeout method -like in the second setTimeout call- the exact same function you refer in the current scope will be executed.