Sunday, May 27, 2018

How do I declare a namespace in JavaScript?



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?


Answer



I like this:



var yourNamespace = {

foo: function() {
},

bar: function() {

}
};

...

yourNamespace.foo();

No comments:

Post a Comment

plot explanation - Why did Peaches' mom hang on the tree? - Movies & TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...