How to provide namespaces in JavaScript with instanced objects

后端 未结 5 2086
爱一瞬间的悲伤
爱一瞬间的悲伤 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:06

    Another alternative may be the bob.js framework:

    bob.ns.setNs('myApp.myFunctions', { 
        say: function(msg) {  
            console.log(msg); 
        } 
    }); 
    
    //sub-namespace
    bob.ns.setNs('myApp.myFunctions.mySubFunctions', { 
        hello: function(name) { 
            myApp.myFunctions.say('Hello, ' + name); 
        } 
    }); 
    
    //call:
    myApp.myFunctions.mySubFunctions.hello('Bob'); 
    

提交回复
热议问题