代码之家  ›  专栏  ›  技术社区  ›  Zoidberg

用于写入javascript对象的最干净格式

  •  1
  • Zoidberg  · 技术社区  · 16 年前

    写JavaScript对象最干净的格式是什么?

    目前我用以下格式写我的

    if (Namespace1 == null) var Namespace1 = {};
    if (Namespace1.NameSpace2 == null) Namespace1.NameSpace2 = {};
    
    Namespace1.NameSpace2.Class1 = function(param1,param2){
         // define private instance variables and their getters and setters
         var privateParam = param1;
         this.getPrivateParam = function() {return privateParam;}
         this.publicParam1 = param2;
    }
    
    Namespace1.Namespace2.Class1.prototype = {
         publicParam1:null,
         publicFunction1:function() {/* Function body*/}
    }
    

    这种格式现在工作得很好,因为yui文档软件能够解析它,并提供注释和良好的文档。但它没有提供一种清晰的方法来声明命名空间中的静态全局方法。我还想知道是否有一种更清晰的方法来声明私有变量。

    我的问题是,是否有人比这更清楚地定义JavaScript对象,如果是这样,为什么你的方法更好?

    谢谢!

    4 回复  |  直到 13 年前
        1
  •  5
  •   Olivieri    16 年前

    模块模式可以帮助您:

     var Namespace1 = Namespace1 || {};
        Namespace1.Namespace2 = Namespace1.Namespace2 || {};
    
        Namespace1.Namespace2.Class1 = function(param1, param2) {
            // define private instance variables and their getters and setters
            var privateParam = param1;
            this.getPrivateParam = function() { return privateParam; }
            this.publicParam1 = param2;
    
            return {
                init: function() {
                    alert('hi from Class1');
                }
            }
        } ();
    

    您可以在这里阅读更多信息: http://yuiblog.com/blog/2007/06/12/module-pattern/

        Namespace1.Namespace2.Class1.init();
    
        2
  •  5
  •   Eli Grey    16 年前

    首先,如果你不知道 Namespace1 定义、使用 typeof this.Namespace1 !== "undefined" ,因为如果未定义Namespace1,则访问它将引发错误。此外,未定义的属性是 undefined 不是 null 虽然 undefined == null )如果有东西真的是 无效的 . 如果你不想使用 typeof 用于检查属性是否未定义,请使用 myObject.property === undefined .

    另外,第二个示例的语法无效。我认为你想做的是:

    Namespace1.Namespace2.Class1.prototype = {
         publicParam1    : null,
         publicFunction1 : function () {/* Function body*/}
    };
    
        3
  •  1
  •   Kenan Banks    16 年前

    当然要重写前两行:

    var Namespace1 = Namespace1 || {};
    Namespace1.Namespace2 = Namespace1.Namespace2 || {};
    

    其他的看起来还可以。私有变量几乎就是每个人都做的。静态方法应分配给 prototype ,正如您所做的。

    注意重新定义 整个的 不过,对象的原型,因为它将阻止您使用 原型 -基于继承。例如:

    // You inherit like this...
    Sub.prototype = new Super();
    obj = new Sub();
    
    // Then you overwrite Sub.prototype when you do this:
    Sub.prototype = {foo:1, bar:2}
    
    // Instead, you should assign properties to the prototype individually:
    Sub.prototype.foo = 1;
    Sub.prototype.bar = 2;
    
        4
  •  1
  •   Alex    13 年前

    我使用以下功能:

    jQuery.namespace = function() {
        var a = arguments, o = null, i, j, d;
        for (i=0; i<a.length; i=i+1) {
            d = a[i].split(".");
            o = window;
            for (j=0; j<d.length; j=j+1) {
                o[d[j]] = o[d[j]] || {};
                o = o[d[j]];
            }
        }
        return o;
    }
    

    然后我可以使用此函数创建这样的名称空间:

    $.namespace("jQuery.namespace1");
    

    一旦我创建了名称空间,我就可以声明函数或任何你想要的函数:

    函数:

    $.namespace1.asyncRequest = function() {
        [function body]
    };
    

    常数:

    $.namespace1.OFFSET = 10;
    

    对象:

    $.namespace1.request = { requestId: 5, protocol: 'JSON' };
    

    我觉得它很简单,很优雅。

    再见, 亚历克斯