括号中的代码块在javascript / jquery中是什么意思? [英] What does a block of code in parentheses mean in javascript/jquery?
问题描述
可能重复:
jQuery:什么是(function($){})(jQuery);是什么意思?
我看过很多jQuery代码,的语法,但我真的不明白这是什么意思。它显示在此答案和这个回答关于代码组织的问题。两个都谈论命名空间,所以我猜这是它完成的。
var foo =(function(){
var someVar;
function someFunc(){
return true;
}
})();
这是命名空间,它是如何工作的?有时在最后一组括号中有一个名称(命名空间?),有时不是。 c>( c>) c> wrap函数将匿名函数声明转换为函数表达式,然后可以使用表达式后面的()
立即调用。
在这种情况下,外部()
真的不是必需的,因为 var foo =
把它变成一个表达式。此外, foo
的值将为 undefined
,因为函数调用不返回任何内容。
它可以用于创建一个新的变量范围,因为一个函数是在javascript中完成它的唯一方法。 (Javascript没有块范围。)
因此, someVar
变量不能访问外部范围。可能有时需要使其以受控的方式可及。为此,您可以传递一个引用 someVar
的范围的函数。然后在函数调用退出后,它的执行上下文将保持不变,并且 someVar
将以任何你传递的函数提供的方式可用。
这叫做创建闭包
。
假设你在调用中传递了一个值,并将它赋给 someVar
。然后,您可以从调用 foo
变量中 return
一个函数。如果该函数返回引用 someVar
,那么您可以使用该函数获取其值。
var foo =(function(str){
var someVar = str;
/ *
function someFunc(){
return true;
}
* /
return function(){
alert(someVar);
};
})('somevalue');
foo(); // alert'somevalue'
如你所见, foo
仍然可以访问 someVar
。
函数返回到 foo
可以接受一个参数,它将更新 myVar
的值。
var foo =(function(str){
var someVar = str;
/ *
function someFunc
return true;
}
* /
return function(n){
if(n){
someVar = n;
} else {
alert(someVar);
}
};
})('somevalue');
foo(); // alerts'somevalue'
foo('newvalue'); //给它一个新值
foo(); // warning'newvalue'
现在你可以看到 foo
确实访问该变量,因为它能够更改其值,并引用它先前设置的新值。
Possible Duplicate:
jQuery: What does (function($) {})(jQuery); mean?
I've seen a lot of jQuery code with the following sort of syntax, but I don't really understand what it means. It shows up in this answer and this answer on a question about code organization. Both talk about namespacing, so I'm guessing that's what it accomplishes.
var foo = (function () {
var someVar;
function someFunc() {
return true;
}
})();
Is this for namespacing, and how does it work? Sometimes there is a name (the namespace?) in the final set of parentheses, sometimes not. What is the difference between the two?
The ()
that wrap the function turns the anonymous function declaration into a function expression that can then be immediately invoked with the ()
that follows the expression.
In this case, the outer ()
really isn't necessary since the var foo =
would turn it into an expression. Also, the value of foo
will be undefined
since the function invocation doesn't return anything.
It can be used for creating a new variable scope, since a function is the only way to accomplish that in javascript. (Javascript doesn't have block scope.)
So the someVar
variable is not accessible to the outer scope. There may be times when it is desirable to make it accessible in a controlled manner. To do this, you can pass a function out of that scope which references someVar
. Then after the function invocation exits, its execution context will remain intact, and someVar
will be available in whatever manner the function you passed out provides.
This is called creating a closure
.
Let's say you passed a value into the invocation, and assigned it to someVar
. You could then return
a function out of the invocation to the foo
variable. If that function you return references someVar
, then you could use that function to get its value.
var foo = (function ( str ) {
var someVar = str;
/*
function someFunc() {
return true;
}
*/
return function() {
alert( someVar );
};
})( 'somevalue' );
foo(); // alerts 'somevalue'
As you can see, the function now referenced by foo
can still access someVar
.
Let's say you changed it so that the function returned to foo
can accept an argument, which will update the value of myVar
.
var foo = (function ( str ) {
var someVar = str;
/*
function someFunc() {
return true;
}
*/
return function( n ) {
if( n ) {
someVar = n;
} else {
alert( someVar );
}
};
})( 'somevalue' );
foo(); // alerts 'somevalue'
foo( 'newvalue' ); // give it a new value
foo(); // alerts 'newvalue'
Now you can see, that the function in foo
really does access that variable, as it is able to change its value, and reference the new value that it previously set.
这篇关于括号中的代码块在javascript / jquery中是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!