什么 (function (x,y){...})(a,b); 在 JavaScript 中是什么意思?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3921922/
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-10-25 02:42:03  来源:igfitidea点击:

What does (function (x,y){...})(a,b); mean in JavaScript?

javascript

提问by user285020

I saw this function:

我看到了这个功能:

(function (x, y, data, lbl, dot) {
    // Function body...
})(x, y, data[i], labels[i], dot);

What is this? A function? Why place a function definition in ()?

这是什么?一个函数?为什么要在 中放置函数定义()

回答by Nivas

In javascript you can have anonymousand self invokingfunctions.

在javascript中,您可以拥有anonymousself invoking功能。

function add(a, b)
{
   return a + b;
}

is same as

var add = function (a, b) {
             return a + b;
          }

and you call these as

你把这些称为

add(10, 20)

You can define the function and call it immediately as

您可以定义该函数并立即调用它

(
   function(a, b)
   {
      return a + b;
   }
)(10, 20);

The

   (
       function(a, b)
       {
          return a + b;
       }
    )

part defines a function, and the (10, 20)immediately after it calls the function just defined, with 10 and 20 as arguments to it.

part 定义了一个函数,(10, 20)紧接着它调用刚刚定义的函数,用 10 和 20 作为它的参数。

Since the function does not have a name, it cannot be used later in the code.

由于该函数没有名称,所以后面的代码中不能使用它。

The code in your question is probably minified, and creates a function in a similar way and calls it immediately.

您问题中的代码可能是minified,并以类似的方式创建一个函数并立即调用它。

回答by Gumbo

function() {}is a definition of an anonymous functionand (function() {})()is a call of that anonymous function.

function() {}是一个匿名函数的定义(function() {})()是匿名函数的调用。

This works since functions can be passed like data. So window.alertis the known alertfunction itself and window.alert()will call that function.

这是有效的,因为函数可以像数据一样传递。所以window.alert是已知alert函数本身window.alert()会调用该函数。

This technique is often used to keep the current variable scope clean as the function has its own variable scope.

这种技术通常用于保持当前变量范围干净,因为函​​数有自己的变量范围。

回答by Daniel Vassallo

It is a self anonymous invoking function. The function is defined and executed immediately. The parenthesis that wrap the function ensure that it is treated as a function expressioninstead of a function declaration. The final pair of parenthesis invoke the function and pass the arguments.

它是一个自匿名调用函数。该函数被定义并立即执行。包装函数的括号确保它被视为函数表达式而不是函数声明。最后一对括号调用函数并传递参数。

回答by Jacob Relkin

A self-executing anonymous function would be a pretty accurate description.

自执行匿名函数将是一个非常准确的描述。

回答by Q_Mlilo

It is a self invoking function, it is executed right away. Self invoking functions are effective for avoiding creation of global variables, jQuery uses this very effectively.

它是一个自调用函数,它会立即执行。自调用函数对于避免创建全局变量非常有效,jQuery 非常有效地使用了这一点。

回答by DoXicK

var funct = function(x,y) { }
funct(1,2)

is the same as

是相同的

(function(x,y){ })(1,2);

it defines a self invoking anonymous function. It gets executed and then thrown away. It is a way of tidying your code (although this is a bad example) and having private variables in only that scope. It also won't get stored within the closure.

它定义了一个自调用匿名函数。它被执行然后扔掉。这是一种整理代码(尽管这是一个不好的例子)并仅在该范围内拥有私有变量的方法。它也不会存储在闭包中。

回答by vhallac

The function inside the parenthesis is an anonymous function. I cannot say why this is done the way it is done, but the programmer defines and anonymous function, and calls it immediately. You could probably do the same thing by simply substituting its arguments with the values passed in, and removing the function definition.

括号内的函数是匿名函数。我不能说为什么这样做是这样做的,但程序员定义了匿名函数,并立即调用它。您可以通过简单地用传入的值替换其参数并删除函数定义来做同样的事情。