简体   繁体   English

javascript组织

[英]javascript organisation

I came across this Q/A on javascript code organisation. 我在javascript代码组织上遇到了这个Q / A.

var DED = (function() {

var private_var;

function private_method()
{
    // do stuff here
}

return {
    method_1 : function()
        {
            // do stuff here
        },
    method_2 : function()
        {
            // do stuff here
        }
};
})();

Currently I do this, 目前我这样做,

 var DED = new Object;
 DED = {
            sidebar : {
                      method_1 : function (data){
                                //some stuff
                                },
                      method_2 : function(data){
                                //do more
                                }
            },
            main : {
                   //.......
            },
            globalVariables : {
                   //...
            }

 }

What is the advantage of one over the other? 一个优于另一个的优势是什么?
Warning: newbie here. 警告:新手在这里。

As indicated, that method uses closures to implement private functions and data. 如上所述,该方法使用闭包来实现私有函数和数据。 It's an alternative to the constructor method (below). 它是构造函数方法的替代方法(如下所示)。 Eg 例如

var DED = new (function()
{
  var private_var;

  function private_method()
  {
    // do stuff here
  }

  this.method_1 = function()
  {
    // do stuff here
  };

  this.method_2 = function()
  {
    // do stuff here
  };
})();

With the DED method shown in your question, there is no constructor. 使用您的问题中显示的DED方法,没有构造函数。 Rather the function returns an object created from an object literal. 而是该函数返回从对象文字创建的对象。 The functions in that object have the private variable and method closed into them. 该对象中的函数将私有变量和方法封闭在其中。

What you return from the anonymous self-calling function (function(){})() is the interface you publish for your "module" (DED). 您从匿名自调用函数(function(){})()返回的是您为“模块”(DED)发布的接口。

DED.method_1() is public. DED.method_1()是公开的。 private_method/private_var are not accessible from outside but everything inside of your self-calling function has access to them. private_method / private_var不能从外部访问,但是自我调用函数内部的所有内容都可以访问它们。

If you like this kind of access-control this is a good way to prevent other developer from accidentally messing with the internals of your module. 如果您喜欢这种访问控制,这是防止其他开发人员意外搞乱模块内部的好方法。 In a lot of cases i'd just go for a naming convention like a leading underscore to indicate internals. 在很多情况下,我只是选择一个命名约定,如前导下划线表示内部。

Javascript is very dynamic and if someone really wants to mess with code they have no write-access to they will be able to do so. Javascript是非常动态的,如果有人真的想弄乱代码,他们没有写访问权限,他们就可以这样做。 Edit : This turns out to be a wrong assuption and not the case for private data in constructors or closures. 编辑 :这是一个错误的假设,而不是构造函数或闭包中的私有数据的情况。 Please, see: http://www.crockford.com/javascript/private.html 请参阅: http//www.crockford.com/javascript/private.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM