问题描述
我始终在javascript源代码中看到这一点,但我从未真正发现过使用此构造的真正原因。为什么需要?
(function(){
// stuff
})();
为什么这样写呢?为什么不单单使用东西
本身而不是函数?
编辑:我知道这是定义一个匿名函数然后调用它,但为什么?
这个函数定义了一个闭包
这用于创建具有私有功能和变量的全局可见的函数闭包。
考虑以下代码:
$ b $ pre $ (function(){
var test = true;
})();
变量 test
在其他地方不可见,但在函数闭包中定义。
什么是闭包?
函数闭包使得它有可能即使它们定义了类似命名的变量或私有函数,各种脚本也不会相互干扰。这些私人物品仅在封闭物本身中可见并且可以访问,而不是在封闭物之外。
检查下面的代码并阅读评论:
// public part
var publicVar = 111;
var publicFunc = function(value){alert(value); };
var publicObject = {
//无函数
};
//关闭部分
(函数(pubObj){
//私有变量和函数
var closureVar = 222;
var closureFunc = function值){
//调用public func
publicFunc(value);
//提醒私有变量
alert(closureVar);
};
//将函数添加到访问私有功能的公共对象
pubObj.alertValues = closureFunc;
//介绍使其成为公共变量的缺失var
anotherPublic = 333;
})(publicObject);
//警报111& alert 222
publicObject.alertValues(publicVar);
//尝试访问varaibles
alert(publicVar); //警报111
alert(anotherPublic); // alert 333
alert(typeof(closureVar)); // alertundefined
以下是,它显示数据,如上面代码中的注释所示。
它实际上做了什么?
正如您已经知道的那样
-
会创建一个函数:
function(){...}
-
立即执行它:
(func)();
-
这个函数可能接受也可能不接受其他参数。
jQuery插件通常以这种方式定义,通过定义一个带有一个插件操作参数的函数:
(function(paramName){...})(jQuery);
但是主要思想仍然是一样的:使用私有定义定义函数闭包,可以直接在它之外使用。
I see this all the time in javascript sources but i've never really found out the real reason this construct is used. Why is this needed?
(function() {
//stuff
})();
Why is this written like this? Why not just use stuff
by itself and not in a function?
EDIT: i know this is defining an anonymous function and then calling it, but why?
This defines a function closure
This is used to create a function closure with private functionality and variables that aren't globally visible.
Consider the following code:
(function(){
var test = true;
})();
variable test
is not visible anywhere else but within the function closure where it's defined.
What is a closure anyway?
Function closures make it possible for various scripts not to interfere with each other even though they define similarly named variables or private functions. Those privates are visible and accessible only within closure itself and not outside of it.
Check this code and read comments along with it:
// public part
var publicVar = 111;
var publicFunc = function(value) { alert(value); };
var publicObject = {
// no functions whatsoever
};
// closure part
(function(pubObj){
// private variables and functions
var closureVar = 222;
var closureFunc = function(value){
// call public func
publicFunc(value);
// alert private variable
alert(closureVar);
};
// add function to public object that accesses private functionality
pubObj.alertValues = closureFunc;
// mind the missing "var" which makes it a public variable
anotherPublic = 333;
})(publicObject);
// alert 111 & alert 222
publicObject.alertValues(publicVar);
// try to access varaibles
alert(publicVar); // alert 111
alert(anotherPublic); // alert 333
alert(typeof(closureVar)); // alert "undefined"
Here's a JSFiddle running code that displays data as indicated by comments in the upper code.
What it actually does?
As you already know this
creates a function:
function() { ... }
and immediately executes it:
(func)();
this function may or may not accept additional parameters.
jQuery plugins are usually defined this way, by defining a function with one parameter that plugin manipulates within:
(function(paramName){ ... })(jQuery);
But the main idea is still the same: define a function closure with private definitions that can't directly be used outside of it.
这篇关于为什么这个函数用圆括号括起来,然后用括号括起来?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!