admin管理员组文章数量:1336633
What does the following () do? It seems prints out the content inside ().
(function() {
console.log("test");
});
This is how ExtJS 4 defines the version:
(function() {
// Current core version
var version = '4.1.1', Version;
Ext.Version = Version = Ext.extend(Object, {
...
});
What does the following () do? It seems prints out the content inside ().
(function() {
console.log("test");
});
This is how ExtJS 4 defines the version:
(function() {
// Current core version
var version = '4.1.1', Version;
Ext.Version = Version = Ext.extend(Object, {
...
});
Share
edited Oct 29, 2012 at 18:57
Frédéric Hamidi
263k42 gold badges495 silver badges484 bronze badges
asked Oct 29, 2012 at 18:40
J AnyJ Any
5,0573 gold badges15 silver badges8 bronze badges
6
- There is no following () here... but if there was, it's to execute the anonymous function preceding it. – asawyer Commented Oct 29, 2012 at 18:41
- 3 It won't do much, as it is not called. – Frédéric Hamidi Commented Oct 29, 2012 at 18:41
- 1 You probably wanted this: (function() { console.log("test"); })(); – Kiran Commented Oct 29, 2012 at 18:42
-
Some context, please. Do you know what
console.log
is? – Bergi Commented Oct 29, 2012 at 18:42 - Could you provide some context? Wouldn't this happen to be part of an assignment or string concatenation? – Fabrício Matté Commented Oct 29, 2012 at 18:44
4 Answers
Reset to default 9I'm going to have to assume you incorrectly copied code.
This does nothing(function() {
console.log("test");
});
This writes "test" to the console
(function() {
console.log("test");
}());
The real question it seems like you're asking is, why.
function () {...}
defines a new anonymous function.
function () {...}()
calls the anonymous function
(function () {...}());
parens are used to force the execution of the function. Without an operator of some sort, the JS interpreter would throw a SyntaxError.
The reason someone would put an immediately invoked function expression inline in some code is to introduce a new variable scope. In JavaScript, blocks don't have scope, which can lead to some very confusing errors for new developers who are not aware of the issue.
Additionally, using a function as a wrapper prevents var
and function
declarations from polluting the global namespace, and instead are privately held within the IIFE. This is A Good Thing™.
Given the additional information J Any posted:
"This is how ExtJS 4 defines the version"(function() {
// Current core version
var version = '4.1.1', Version;
Ext.Version = Version = Ext.extend(Object, {
...
});
The code posted is inplete. Braces and parens were not matched correctly.
The end });
belongs to Ext.extend(Object, {
not (function () {
.
It does nothing, that code can never get called since the anonymous function or any other symbols in it are not exposed.
If you add ()
to the end, that is a self calling anonymous function, which is usually used to create some local variables that aren't accessible to the rest of the code
(function() {
console.log("test");
})();
The reason the code that Ext-JS does make sense is because they are making some code inside the anonymous function available to the outside by calling
Ext.Version = Version = Ext.extend(Object, ...
And you have a typo in your sample Ext-JS Code. Their code is actually the following
(function() {
// Current core version
var version = '4.1.1', Version;
// Exposes our local Version into the global Ext.Version object
Ext.Version = Version = Ext.extend(Object, {
//... More code
});
}()) // See the brackets after the function? That runs the code above immediately
Actually, that alone would do nothing. It defines an anonymous function, which would never be able to be referenced or called later.
However, this:
(function() {
console.log("test");
})();
would define an anonymous function and call it, printing "test" to the console. The first bit is the same as what you have in the question, and the last ()
calls the function.
It does nothing. It's a function expression surrounded in parenthesis. Without them, you will get an error.
本文标签: javascriptWhat does (function() ) doStack Overflow
版权声明:本文标题:javascript - What does (function() {}) do? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742401322a2467941.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论