admin管理员组

文章数量:1336340

What is the cleanest format for writing javascript objects?

Currently I write mine in the following format

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*/}
}

That format works well right now, as the YUI documentation software is able to parse it, and the ments and give back good documentation. But what it doesn't provide is a clean way to declare static global methods within the namespace. I am also wondering if there is a cleaner way to declar private variables as well.

My question is, is anyone out there have a cleaner way of defining javascript objects than this, and if so, why is your method better?

Thanks!

What is the cleanest format for writing javascript objects?

Currently I write mine in the following format

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*/}
}

That format works well right now, as the YUI documentation software is able to parse it, and the ments and give back good documentation. But what it doesn't provide is a clean way to declare static global methods within the namespace. I am also wondering if there is a cleaner way to declar private variables as well.

My question is, is anyone out there have a cleaner way of defining javascript objects than this, and if so, why is your method better?

Thanks!

Share Improve this question edited Aug 26, 2009 at 19:33 Zoidberg asked Aug 26, 2009 at 19:00 ZoidbergZoidberg 10.3k2 gold badges33 silver badges53 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 5

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./blog/2007/06/12/module-pattern/

    Namespace1.Namespace2.Class1.init();

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

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

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

本文标签: Cleanest format for writing javascript objectsStack Overflow