函数声明后的空括号()在javascript中做什么?(复制)

时间:2022-03-08 04:37:12

This question already has an answer here:

这个问题已经有了答案:

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

我正在尝试读取原型源。我到这里来了。(不幸的是,这个片段在开头)。

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?

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

4 个解决方案

#1


40  

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

代码正在定义一个匿名函数(函数(){…然后调用它(没有参数)。然后,它将值分配给对象的浏览器属性,该属性可能是在代码段之外定义的。

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).

这样做的一个缺点是,您“污染了您的名称空间”,带有一个函数和一个您在其他任何地方都不会使用的变量。第二个问题是,您不能在函数定义中使用任何本地范围的变量的值(匿名函数作为闭包)。

#2


27  

(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 Browser object. So, you end up with boolean values for, e.g., Browser.IE, Browser.Opera, etc.

对于这个特定的函数,匿名函数向浏览器对象返回几个属性。因此,您最终会得到布尔值,例如,浏览器。例如,浏览器。歌剧等。

#3


11  

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

它调用刚刚声明的匿名函数,有效地导致对“块”进行计算。

#4


5  

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 Browser property.

这是一个简单的函数调用,与foo()没什么不同,只是它调用了一个匿名函数字面量,函数的结果被分配给浏览器属性。

#1


40  

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

代码正在定义一个匿名函数(函数(){…然后调用它(没有参数)。然后,它将值分配给对象的浏览器属性,该属性可能是在代码段之外定义的。

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).

这样做的一个缺点是,您“污染了您的名称空间”,带有一个函数和一个您在其他任何地方都不会使用的变量。第二个问题是,您不能在函数定义中使用任何本地范围的变量的值(匿名函数作为闭包)。

#2


27  

(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 Browser object. So, you end up with boolean values for, e.g., Browser.IE, Browser.Opera, etc.

对于这个特定的函数,匿名函数向浏览器对象返回几个属性。因此,您最终会得到布尔值,例如,浏览器。例如,浏览器。歌剧等。

#3


11  

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

它调用刚刚声明的匿名函数,有效地导致对“块”进行计算。

#4


5  

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 Browser property.

这是一个简单的函数调用,与foo()没什么不同,只是它调用了一个匿名函数字面量,函数的结果被分配给浏览器属性。