rp.
rp.

Reputation: 17683

How to provide namespaces in JavaScript with instanced objects

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

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

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

...

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?

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.

Thanks rp

Upvotes: 11

Views: 9952

Answers (5)

rp.
rp.

Reputation: 17683

Both answers were very helpful! Here's what I ended up with:

if( typeof( rpNameSpace ) == "undefined" ) rpNameSpace = {};

rpNameSpace.foo = function() {
    this.length = 613;
}
rpNameSpace.foo.prototype.getLength = function() {
    return this.length * 2;
}

Then, to use the resulting "namespaced" object:

var x = new rpNameSpace.foo()

display( x.getLength() );

Upvotes: 2

Marius
Marius

Reputation: 59009

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.

For example:

var namespace = {
  this.foo: function(){
    ...
  },
  this.foo.prototype.getLength: function(){
    ...
  }
}

You could also create a set of nested objects and simulate packages:

loadPackage = function(){
  var path = arguments[0];
  for(var i=1; i<arguments.length; i++){
    if(!path[arguments[i]]){
      path[arguments[i]] = {};
    }
    path = path[arguments[i]];
  }
  return path;
}

loadPackage(this, "com", "google", "mail") = {
  username: "gundersen",
  login: function(password){
    ...
  }
}
this.com.google.mail.login("mySecretPassword");

Upvotes: 8

Tengiz
Tengiz

Reputation: 8449

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'); 

Upvotes: -1

Jason Bunting
Jason Bunting

Reputation: 58969

Simple:

if(!MyNamespace) MyNamespace = {};

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

Upvotes: 12

Nickolay
Nickolay

Reputation: 32081

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.

Upvotes: 2

Related Questions