<p>I've got a JavaScript "object", built this way:</p>

<pre><code>function foo()
{
    this.length = 0;
}

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

...
</code></pre>

<p>I know how to emulate namespaces with singleton JavaScript objects, but what is the best way to "namepace" an object such as that above that will intanced? </p>

<p>I know that several JavaScript libraries have namepacing capabilities, but I'm using jQuery and would rather not add another library to the mix. I'd like to be able to provide my own, perhaps by exploiting jQuery, intrinsic namespacing scheme for the JS objects of mine that need to be instanced. </p>

<p>Thanks
rp</p>

[{'answer_id': 22998, 'author': 'Nickolay', 'author_id': 1026, 'author_profile': 'https://Stackoverflow.com/users/1026', 'pm_score': 2, 'selected': False, 'text': "<p>Shouldn't be much different:</p>\n\n<pre><code>namespace.foo = function foo() {...}\nnamespace.foo.prototype.getLength = function() { ... }\n</code></pre>\n\n<p>or you could use</p>\n\n<pre><code>(function() {\n  function foo() { ... }\n  foo.prototype...\n  namespace.foo = foo;\n})();\n</code></pre>\n\n<p>to save some typing.</p>\n"}, {'answer_id': 23089, 'author': 'Jason Bunting', 'author_id': 1790, 'author_profile': 'https://Stackoverflow.com/users/1790', 'pm_score': 5, 'selected': True, 'text': '<p>Simple:</p>\n\n<pre><code>if(!MyNamespace) MyNamespace = {};\n\nMyNamespace.foo = function() {\n   this.length = 0;\n};\nMyNamespace.foo.prototype.getLength = function() {\n   return this.length;\n};\n</code></pre>\n'}, {'answer_id': 23207, 'author': 'Marius', 'author_id': 1585, 'author_profile': 'https://Stackoverflow.com/users/1585', 'pm_score': 3, 'selected': False, 'text': '<p>Javascript doesn\'t really have namespace or packages like other languages. Instead it has closures. If you have an application that consists of multiple functions, variables and objects, then you should put them inside a single global object. This will have the same effect as a namespace. </p>\n\n<p>For example:</p>\n\n<pre><code>var namespace = {\n  this.foo: function(){\n    ...\n  },\n  this.foo.prototype.getLength: function(){\n    ...\n  }\n}\n</code></pre>\n\n<p>You could also create a set of nested objects and simulate packages:</p>\n\n<pre><code>loadPackage = function(){\n  var path = arguments[0];\n  for(var i=1; i&lt;arguments.length; i++){\n    if(!path[arguments[i]]){\n      path[arguments[i]] = {};\n    }\n    path = path[arguments[i]];\n  }\n  return path;\n}\n\nloadPackage(this, "com", "google", "mail") = {\n  username: "gundersen",\n  login: function(password){\n    ...\n  }\n}\nthis.com.google.mail.login("mySecretPassword");\n</code></pre>\n'}, {'answer_id': 23468, 'author': 'rp.', 'author_id': 2536, 'author_profile': 'https://Stackoverflow.com/users/2536', 'pm_score': 2, 'selected': False, 'text': '<p>Both answers were very helpful! Here\'s what I ended up with: </p>\n\n<pre><code>if( typeof( rpNameSpace ) == "undefined" ) rpNameSpace = {};\n\nrpNameSpace.foo = function() {\n    this.length = 613;\n}\nrpNameSpace.foo.prototype.getLength = function() {\n    return this.length * 2;\n}\n</code></pre>\n\n<p>Then, to use the resulting "namespaced" object:</p>\n\n<pre><code>var x = new rpNameSpace.foo()\n\ndisplay( x.getLength() );\n</code></pre>\n'}, {'answer_id': 15820688, 'author': 'Tengiz', 'author_id': 523949, 'author_profile': 'https://Stackoverflow.com/users/523949', 'pm_score': -1, 'selected': False, 'text': '<p>Another alternative may be the <a href="http://www.bobjs.com" rel="nofollow">bob.js</a> framework:</p>\n\n<pre><code>bob.ns.setNs(\'myApp.myFunctions\', { \n    say: function(msg) {  \n        console.log(msg); \n    } \n}); \n\n//sub-namespace\nbob.ns.setNs(\'myApp.myFunctions.mySubFunctions\', { \n    hello: function(name) { \n        myApp.myFunctions.say(\'Hello, \' + name); \n    } \n}); \n\n//call:\nmyApp.myFunctions.mySubFunctions.hello(\'Bob\'); \n</code></pre>\n'}]
['https://Stackoverflow.com/questions/22976', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/2536/']