How to provide namespaces in JavaScript with instanced objects

后端 未结 5 2075
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-02-03 10:42

I\'ve got a JavaScript \"object\", built this way:

function foo()
{
    this.length = 0;
}

foo.prototype.getLength = function()
{
    return this.length;
}

...         


        
5条回答
  •  挽巷
    挽巷 (楼主)
    2021-02-03 11:21

    Simple:

    if(!MyNamespace) MyNamespace = {};
    
    MyNamespace.foo = function() {
       this.length = 0;
    };
    MyNamespace.foo.prototype.getLength = function() {
       return this.length;
    };
    

提交回复
热议问题