为什么括号用于包装javascript函数调用? [英] Why are parenthesis used to wrap a javascript function call?

查看:114
本文介绍了为什么括号用于包装javascript函数调用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这两个javascript函数调用有什么区别?

What is the difference between these two javascript function calls?

(function(){alert("foo")})()

与此相对:

(function(){alert("foo")}()) 


推荐答案

这样做是为了便于阅读。

两者之间没有真正的功能差异你给出的两个例子,但它们都非常接近一个简单的函数声明, 不同。添加括号是为了便于辨别。

There isn't a real functional difference between the two examples you've given, but both of them are very close to a simple function declaration, which is different. The parenthesis are added for readability, in order to distinguish them.

以下是每个代码段的作用:

在两个片段的第一个中,第一个括号将被计算为封闭函数的值。然后该值将作为函数调用。所以最终函数将被执行,这可能是你关心的。

In the first of your two snippets, the first parenthesis will be evaluated as the value of the enclosed function. Then this value will be called as a function. So ultimately the function will be executed, which is probably what you care about.

在你的第二个片段中,外括号将被评估为包含一个内联声明的函数并立即执行。同样,该函数将被执行,这仍然可能是您关心的。

In your second snippet, the outer parenthesis will be evaluated as containing a function which is declared inline and immediately executed. Again, the function will be executed, which is still probably what you care about.

这两个函数都将执行相同的函数,因此不会有任何显着差异。

Both of these will execute the same function, so there won't be any significant difference.

像你这样的代码片段与简单的函数声明之间的区别:

您给出的功能也与以下内容相同。我刚刚添加了一个函数名称并为语法准确性分配了返回值,您现在可以忽略它。

The functions you've given are also identical to the following. I've just added a function name and assigned the return value for syntactical accuracy, which you can ignore for now.

// javascript...
var val = 
  function myFooFunc () { 
    alert("foo"); 
  }();

然而,这很容易被误认为是一个简单的函数声明,这是不同的:

However, this would be easily mistaken for a simple function declaration, which is different:

// javascript...
function myFooFunc () { 
  alert("foo"); 
}

请注意,这里唯一真正的区别是最后一个函数声明没有被执行立即。其他人是。所以这是一个非常不同的行为(如果按名称调用简单声明,或者根本不执行,则可以稍后执行)。但是,通常很难看到语法上的差异,特别是如果函数体长得很长并且需要在屏幕上滚动。

Notice that the only real difference here is that this last function declaration is not executed immediately. The others are. So that is a very different behavior (the simple declaration may be executed later if it is called by name, or it may not ever execute at all). It's often hard to see that difference in the syntax right away, however, especially if the function body grows to be very long and requires scrolling on the screen.

为什么函数会立即执行?

当一个函数在声明后立即执行时,该值通常会返回某个值(它可能是转让声明)。有时函数会立即执行,因为它包含内部函数,并用于为包含的语句提供函数范围。

When a function is immediately executed after it is declared, the value is often being returned to something (it may be part of an assignment statement). Sometimes the function is being executed right away because it contains inner functions and is being used to provide functional scope to the inclosed statements.

基本上,人们围绕执行括起括号立即形成(两个片段,以及我的两个片段中的第一个),以便向其他开发者提供视觉提示,即立即调用该函数。它更容易阅读,因为在你到达函数结束之前你可能不会捕到括号(或者完全注意它们)。

Essentially, people wrap parenthesis around the "executed immediately" form (both of your snippets, and the first one of my two) in order to give a visual cue to other developers that the function is being called immediately. It's just easier to read, since you might not catch the parenthesis until you got to the end of the function (or notice them at all).

这篇关于为什么括号用于包装javascript函数调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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