javascript 括号内的函数“(function(){});”在javascript中是什么意思?

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

What does this "(function(){});", a function inside brackets, mean in javascript?

javascriptcoding-stylescopeanonymous-functionjavascript-namespaces

提问by Shaoz

Possible Duplicates:
What does this mean? (function (x,y)){…}){a,b); in JavaScript
What do parentheses surrounding a JavaScript object/function/class declaration mean?

可能的重复:
这是什么意思?(function (x,y)){…}){a,b); 在 JavaScript
中,JavaScript 对象/函数/类声明周围的括号是什么意思?

Hi All

大家好

I don't know what the following does:

我不知道以下是做什么的:

(function(){
  // Do something here
  ...
})(someWord) //Why is this here?;

My questions are:

我的问题是:

  1. What's the meaning of putting a function inside brackets .i.e. (function(){});?
  2. What do the set of brackets do at the end of a function?
  1. 将函数放在方括号 .ie 中是(function(){});什么意思?
  2. 函数末尾的括号集有什么作用?

I usually see these in jquery codes, and some other javascript libraries.

我通常在 jquery 代码和其他一些 javascript 库中看到这些。

回答by pex

You're immediately calling an anonymus function with a specific parameter.

您立即使用特定参数调用匿名函数。

An example:

一个例子:

(function(name){
  alert(name);
})('peter')

This alerts "peter".

这会提醒“彼得”。

In the case of jQuery you might pass jQueryas a parameter and use $in your function. So you can still use jQuery in noConflict-mode but use the handy $:

在 jQuery 的情况下,您可以jQuery作为参数传递并$在您的函数中使用。所以你仍然可以在 noConflict 模式下使用 jQuery,但使用方便的$

jQuery.noConflict()
(function($){
  var obj = $('<div/>', { id: 'someId' });
})(jQuery)

回答by Rocket Hazmat

You are making a function that is immediately being called, with someWordas a parameter.

您正在创建一个立即被调用的函数,并将其someWord作为参数。

回答by Justin Ethier

Basically this lets you declare an anonymous function, and then by enclosing it in parentheses and writing (someWord)you are running the function. You could think of it as declaring an object and then immediately instantiating the object.

基本上,这让您可以声明一个匿名函数,然后通过将其括在括号中并编写(someWord)您正在运行该函数。您可以将其视为声明一个对象,然后立即实例化该对象。

回答by Shadow Wizard is Ear For You

It's used to create anonymous function (function without name that can be "nested" inside other function) and pass argument to that function. The someWord is passed as argument, and the function can read it using the keyword "arguments".

它用于创建匿名函数(没有名称的函数,可以“嵌套”在其他函数中)并将参数传递给该函数。someWord 作为参数传递,函数可以使用关键字“参数”读取它。

Simple example of usage:

简单的用法示例:

function Foo(myval) {
    (function(){
      // Do something here
      alert(arguments[0]);
    })(myval); //pass myval as argument to anonymous function
}
...
Foo(10);

回答by fish2000

It's a way to define an anonymous function and then immediately executing it -- leaving no trace, as it were. The function's scope is truly local. The ()brackets at the end execute the function -- the enclosing brackets are to disambiguate what is being executed.

这是一种定义匿名函数然后立即执行它的方法——就像它一样不留下任何痕迹。该函数的作用域是真正本地的。最后的()方括号执行函数——括起来的方括号用于消除正在执行的内容的歧义。

回答by darioo

Perhaps thispost will help you a bit.

也许这篇文章会对你有所帮助。