什么(function(x,y){...})(a,b);用JavaScript表示? [英] What does (function (x,y){...})(a,b); mean in JavaScript?
问题描述
我看到了这个函数:
(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 ()
?
推荐答案
在javascript中你可以有匿名
和自行调用
函数。
In javascript you can have anonymous
and self invoking
functions.
function add(a, b)
{
return a + b;
}
与
var add = function (a, b) {
return a + b;
}
您将这些称为
add(10, 20)
你可以定义函数并立即将其调用为
You can define the function and call it immediately as
(
function(a, b)
{
return a + b;
}
)(10, 20);
(
function(a, b)
{
return a + b;
}
)
part定义一个函数,并且在调用函数之后立即(10,20)
定义,以10和20为参数。
part defines a function, and the (10, 20)
immediately after it calls the function just defined, with 10 and 20 as arguments to it.
由于该函数没有名称,因此以后不能在代码中使用。
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.
这篇关于什么(function(x,y){...})(a,b);用JavaScript表示?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!