How do I declare a namespace in JavaScript

De openkb
Aller à : Navigation, rechercher

Sommaire

Questions

How do I create a namespace in JavaScript so that my objects and functions aren t overwritten by other same-named objects and functions? I ve used the following:

if (Foo == null || typeof(Foo) != "object") { var Foo = new Object();}

Is there a more elegant or succinct way of doing this?

Answers

I like this:

var yourNamespace = {

    foo: function() {
    },

    bar: function() {
    }
};

...

yourNamespace.foo();

Source

License : cc by-sa 3.0

http://stackoverflow.com/questions/881515/how-do-i-declare-a-namespace-in-javascript

Related

Outils personnels
Espaces de noms

Variantes
Actions
Navigation
Outils