javascript 中函数声明后的空括号 () 有什么作用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2422026/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 00:15:23  来源:igfitidea点击:

What do empty parentheses () after a function declaration do in javascript?

javascript

提问by Teej

I'm trying to read the Prototype source. I've come to this part.(Unfortunately, this snippet is in the beginnning).

我正在尝试阅读 Prototype 源代码。我已经到了这部分。(不幸的是,这个片段是在开头)。

What does this () mean?

这是什么意思?

  Browser: (function(){
    var ua = navigator.userAgent;
    var isOpera = Object.prototype.toString.call(window.opera) == '[object Opera]';
    return {
      IE:             !!window.attachEvent && !isOpera,
      Opera:          isOpera,
      WebKit:         ua.indexOf('AppleWebKit/') > -1,
      Gecko:          ua.indexOf('Gecko') > -1 && ua.indexOf('KHTML') === -1,
      MobileSafari:   /Apple.*Mobile.*Safari/.test(ua)
    }
  })(),

I am referring to the last line before the comma?

我指的是逗号前的最后一行?

采纳答案by Frank Schmitt

The code is defining an anonymous function (the (function (){ ... })bit) and then calling it (with no arguments). It then assigns the value to the Browserproperty of the object that is presumably being defined outside of your code snippet.

代码定义了一个匿名函数((function (){ ... })位),然后调用它(不带参数)。然后,它将值分配给Browser可能在您的代码片段之外定义的对象的属性。

You could also define the function somewhere:

您还可以在某处定义函数:

function myFunction() {
    var ua = navigator.userAgent;
    var isOpera = Object.prototype.toString.call(window.opera) == '[object Opera]';
    return {
      IE:             !!window.attachEvent && !isOpera,
      Opera:          isOpera,
      WebKit:         ua.indexOf('AppleWebKit/') > -1,
      Gecko:          ua.indexOf('Gecko') > -1 && ua.indexOf('KHTML') === -1,
      MobileSafari:   /Apple.*Mobile.*Safari/.test(ua)
}

and then call it:

然后调用它:

var foo = myFunction();

and then assign the value:

然后赋值:

...
Browser: foo,
...

One downside with doing it that way is that you "pollute your namespace" with a function and a variable that you won't use anywhere else. The second issue is that you can't use the value of any locally-scoped variables in your function definition (the anonymous function behaves as a closure).

这样做的一个缺点是你用一个函数和一个你不会在其他任何地方使用的变量“污染你的命名空间”。第二个问题是您不能在函数定义中使用任何局部作用域变量的值(匿名函数表现为闭包)。

回答by Andrew Hedges

(function () {})creates an anonymous function.

(function () {})创建一个匿名函数。

Adding the ()to the end calls the function that was just created.

添加()到最后调用刚刚创建的函数。

In the case of this particular function, the anonymous function returns several properties to the Browserobject. So, you end up with boolean values for, e.g., Browser.IE, Browser.Opera, etc.

在此特定函数的情况下,匿名函数会向Browser对象返回多个属性。因此,您最终会得到布尔值,例如Browser.IEBrowser.Opera, 等。

回答by cobbal

it calls the anonymous function that was just declared, effectively causing the "block" to be evaluated.

它调用刚刚声明的匿名函数,有效地导致“块”被评估。

回答by meder omuraliev

It's a simple function call, no different than foo()except it's invoking an anonymous function literal, the result of the function is assigned to the Browserproperty.

这是一个简单的函数调用,foo()除了调用匿名函数文字之外没有什么不同,函数的结果被分配给Browser属性。