JavaScript匿名构造函数:!function(){}(); [英] JavaScript Anonymous Constructor Function: !function(){}();

查看:195
本文介绍了JavaScript匿名构造函数:!function(){}();的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在JavaScript中看到了创建匿名构造函数的语法:

  var Application = Application || {}; 
!function(window,Application){
Application.property = {/*...*/}
Application.method = function(){/*...*/}
}(window,Application);

我想了解以下部分:


  1. 使用第一行的优点(即 var o = o || {}; )vs只是说明 var o =(function(){})();

  2. 为什么 c
  3. >
    这是最方便的匿名构造函数的方法,它是如何优化的:

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:

  1. What is the advantage of using first line (i.e. var o = o || {};) vs just stating var o = (function(){})();?
  2. Why ! is used in front of function?
  3. Why would I pass window or Application as parameters when they are global object?
  4. 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 that Application 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 where Application does not yet exist. In all other cases, your code will obliterate the existing Application, 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 currently undefined, 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 and Application into the function and use it there? This is a safety feature, in case other code changes window or Application later on. It guarantees that within the anonymous function, window and Application 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 then window and Application would be stored in the closure, so you'd retain the variables. It makes it safe from people who later on decide to say Application = {...}.

这篇关于JavaScript匿名构造函数:!function(){}();的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆