了解javascript函数调用和引用 [英] understanding javascript function call and referance

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

问题描述

我发现此浏览:

函数之间的区别是什么调用和函数引用?

在阅读完答案后,我不了解函数引用和函数调用的定义和用法.然后,我进行了很多搜索,但仍不清楚在哪里使用.

After reading the answers there, I did't understand the definition and usage for function references and function calls. Then I searched a lot, but it is still unclear where to use what.

您能通过指出概念和用法上的差异来帮助我理解这一点吗?我想以此作为将来程序员的参考.

Could you help me understand this by pointing out the differences in concept and usage? I want to make this as a reference for future programers.

推荐答案

以这个为例:

function foo() {
    alert('foo');
    return 'bar';
}

首先,什么是函数?这是一个可以被称为 (或调用"或执行")的例程,当您这样做时,它通常做某事,并且返回一些值.

First of all, what is a function? It's a routine that can be called (or "invoked", or "executed"), and when you do so it usually does something, and returns some value.

因此,您有一个名为foo的函数.您可以通过在其名称后添加()来调用它:

So you have a function named foo. You can call it by adding () after its name:

foo();

如果将调用结果分配给它,则可以将返回值存储在变量中:

You can store the return value in a variable, if you assign the result of the invocation to it:

var something = foo();
something === 'bar'; // true

但是,这并不是您可以使用JavaScript中的函数所能做的全部.这是一种函数是一等公民的语言,因此可以将它们传递给其他函数,并从其他函数返回.它们也可以存储为变量.例如:

But that's not all you can do with functions in JavaScript. It's a language where functions are first class citizens, so they can be passed around to other functions, and returned from other functions. They can also be stored as variables. For example:

var refToFoo = foo;

现在refToFoofoo相同.它不是副本,而是 reference ,它指向与foo相同的(内部)函数对象.因此,您可以像使用foo一样使用refToFoo:

Now refToFoo is the same as foo. It's not a copy, it's a reference pointing to the same (internal) function object as foo. So you can use refToFoo just like you would use foo:

var something = refToFoo();
something === 'bar'; // true
refToFoo === foo; // true; they're the same object

也许函数引用最常见的用途是将它们用作事件侦听器:

Perhaps the most common use to function references is to use them as event listeners:

someElement.onclick = foo;

请注意,上面没有括号.如果使用括号,则会立即调用foo,并将其返回值分配给元素的onclick方法.由于该函数返回一个字符串,因此如果单击该元素,则不会发生任何事情.这是新手经常犯的错误.另一个常见的做法是调用一个函数,而不是传递对setTimeout的引用:

Note there is no parentheses above. It we used parentheses, foo would be invoked, immediately, and its return value would be assigned to the element's onclick method. Since that function returns a string, nothing would happen if the element were clicked. That's a very common mistake newbies do. Another common one is invoking a function instead of passing a reference to setTimeout:

setTimeout(foo(), 1000); // WRONG - foo is executed immediately

将其与以下内容进行比较:

Compare that to:

setTimeout(foo, 1000); // RIGHT - we're passing a reference to the function, 
                       // that will be invoked by the js engine after 1000ms

我希望这有助于澄清您的疑问.

I hope this helps clarify your doubt.

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

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