JavaScript匿名构造函数:!function(){}(); [英] JavaScript Anonymous Constructor Function: !function(){}();
问题描述
我在JavaScript中看到了创建匿名构造函数的语法:
var Application = Application || {};
!function(window,Application){
Application.property = {/*...*/}
Application.method = function(){/*...*/}
}(window,Application);
我想了解以下部分:
- 使用第一行的优点(即
var o = o || {};
)vs只是说明var o =(function(){})();
? - 为什么
!
c >
这是最方便的匿名构造函数的方法,它是如何优化的:
4a)
var Application = {
property:{},
method:function(){}
}
>
var Application =(function(){
var method = function(){/*...*/}
return {method:method};
}());
解决方案第一行是确保
应用程序
总是存在,通常用于预期应用程序
已经存在的情况,并且该函数只是增加现有对象。如果它不存在,这确保我们没有访问未定义的属性的错误。您的示例仅在应用程序
不存在的情况下等效。在所有其他情况下,您的代码将删除现有的应用程序
,这几乎肯定不是意图。
来自Vatev的评论解释了
!
的作用。这是另一种语法,使有问题的函数成为一个自执行匿名函数。 (顺便说一下,它也接受函数的返回值 - 当前undefined
,并且它的真实性被翻转,所以它的计算结果为true,因为结果不存储)
最后,为什么通过
window
和应用程序
到函数中并使用它?这是一个安全功能,以防日后其他代码更改窗口
或应用程序
。它保证在匿名函数中,窗口
和应用程序
正是你期望的。在你给出的简写示例中,这可能似乎并不重要 - 毕竟,为什么保护这些变量,如果你立即使用它们,而不是存储它们?在很多情况下,你从这个函数返回一些东西,然后window
和Application
你会保留变量。它使得后来决定说Application = {...}
的人安全。I've seen this syntax for creating an anonymous constructor function in JavaScript:
var Application = Application || {}; !function(window, Application) { Application.property = {/*...*/} Application.method = function(){/*...*/} }(window, Application);
I want to understand what the following parts here:
- What is the advantage of using first line (i.e.
var o = o || {};
) vs just statingvar o = (function(){})();
?- Why
!
is used in front of function?- Why would I pass
window
orApplication
as parameters when they are global object?- Is this the most convenient way for anonymous constructor function and how is this better than:
4a)
var Application = { property: {}, method: function(){} }
or 4b)
var Application = (function() { var method = function(){/*...*/} return {method:method}; }());
解决方案The first line is to ensure that
Application
always exists, and is generally used in cases where it's expected thatApplication
already should exist, and the function just augments the existing object. If it doesn't exist, this makes sure that we don't get an error for accessing properties of undefined. Your examples are only equivalent in the case whereApplication
does not yet exist. In all other cases, your code will obliterate the existingApplication
, which is almost certainly not the intent.The comment from Vatev explains what the
!
does. It's another syntax for making the function in question become a self executing anonymous function. (Incidentally, it also takes the return value of the function - which is currentlyundefined
, and flips its truthyness, so it evaluates as true. Since the result isn't stored in any variable, though, that's clearly not the purpose.)Finally, why pass
window
andApplication
into the function and use it there? This is a safety feature, in case other code changeswindow
orApplication
later on. It guarantees that within the anonymous function,window
andApplication
are exactly what you expect it to be. In the shorthand example you gave, this may appear to not matter - after all, why protect these variables if you're using them immediately and not storing them? In many cases, you return something from this function, and thenwindow
andApplication
would be stored in the closure, so you'd retain the variables. It makes it safe from people who later on decide to sayApplication = {...}
.这篇关于JavaScript匿名构造函数:!function(){}();的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!