Cleanest format for writing javascript objects

前端 未结 4 509
面向向阳花
面向向阳花 2021-02-06 19:20

What is the cleanest format for writing javascript objects?

Currently I write mine in the following format

if (Namespace1 == null) var Namespace1 = {};
i         


        
相关标签:
4条回答
  • 2021-02-06 20:01

    Certainly rewrite the first two lines to this:

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

    The rest of the looks ok. The private variable is pretty much how everyone does it. Static methods should be assigned to the prototype, as you have done.

    Do take care redefining the entire prototype for an object though, since it will prevent you from a common pattern of prototype-based inheritance. For instance:

    // 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;
    
    0 讨论(0)
  • 2021-02-06 20:06

    The module pattern may help you out here:

     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');
                }
            }
        } ();
    

    You can read more about it here: http://yuiblog.com/blog/2007/06/12/module-pattern/

        Namespace1.Namespace2.Class1.init();
    
    0 讨论(0)
  • 2021-02-06 20:15

    I use the following function:

    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;
    }
    

    Then I can use this function to create a namespace just like this:

    $.namespace("jQuery.namespace1");
    

    Once I've created the namespace I can declare functions or whatever you want inside it:

    a function:

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

    a constant:

    $.namespace1.OFFSET = 10;
    

    an object:

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

    I think it's simple and elegant :-)

    Bye, Alex

    0 讨论(0)
  • 2021-02-06 20:16

    First of all, if you don't know if Namespace1 is defined, use typeof this.Namespace1 !== "undefined", as accessing Namespace1 will throw an error if it's not defined. Also, undefined properties are undefined, not null (though undefined == null). Your check will fail if something is actually null. If you don't want to use typeof for checking if properties are undefined, use myObject.property === undefined.

    Also, your second example has invalid syntax. Here is what I think you wanted to do:

    Namespace1.Namespace2.Class1.prototype = {
         publicParam1    : null,
         publicFunction1 : function () {/* Function body*/}
    };
    
    0 讨论(0)
提交回复
热议问题