How to provide namespaces in JavaScript with instanced objects

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

    Shouldn't be much different:

    namespace.foo = function foo() {...}
    namespace.foo.prototype.getLength = function() { ... }
    

    or you could use

    (function() {
      function foo() { ... }
      foo.prototype...
      namespace.foo = foo;
    })();
    

    to save some typing.

提交回复
热议问题