“()”的含义是什么?在函数调用? [英] What's the meaning of "()" in a function call?

查看:310
本文介绍了“()”的含义是什么?在函数调用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Now, I usually call a function (that requires no arguments) with () like this:

myFunction(); //there's empty parens

除了在jQuery调用中我可以逃脱:

Except in jQuery calls where I can get away with:

$('#foo').bind('click', myFunction); //no parens

很好。但最近我在SO上看到了这里的评论:

Fine. But recently I saw this comment here on SO:

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

"Consider using setTimeout(monitor, 100); instead of setTimeout('monitor()', 100);. Eval is evil :)"

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

Yikes! Are we really eval()-ing a string here? I guess I don't really understand the significance and implications of 'calling' a function. What are the real rules about calling and referring to functions?

推荐答案

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

In JavaScript functions are first-class objects. That means you can pass functions around as parameters to a function, or treat them as variables in general.

假设我们说的是函数你好

function hello() {
    alert('yo');
}

当我们写简单时

hello

我们指的是没有的功能执行它的内容。但是当我们在函数名后添加parens ()时,

we are referring to the function which doesn't execute it's contents. But when we add the parens () after the function name,

hello()

然后我们实际调用的功能会在屏幕上提示yo。

then we are actually calling the function which will alert "yo" on the screen.

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

The bind method in jQuery accepts the type of event (string) and a function as its arguments. In your example, you are passing the type - "click" and the actual function as an argument.

你见过Inception吗?考虑这个可能使事情更清晰的人为例子。由于函数是JavaScript中的第一类对象,我们可以在函数内传递和返回函数。因此,让我们创建一个在调用时返回函数的函数,并且返回的函数在调用时也会返回另一个函数。

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');
        }
    };
}

这里现实是一个函数, reality()是一个函数, reality()也是一个函数。但是 reality()()()不是函数,而只是 undefined 因为我们没有返回函数(我们没有从最里面的函数返回任何东西。

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.

所以对于现实函数的例子,你可能有将以下任何内容传递给jQuery的bind。

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()());

这篇关于“()”的含义是什么?在函数调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆