Can anyone explain namespace in javascript with an example?
javascript
Solution
There is no official concept of a namespace in Javascript like there is in C++. However, you can wrap functions in Javascript objects to emulate namespaces. For example, if you wanted to write a function in a "namespace" called `MyNamespace`, you might do the following:
var MyNamespace = {};
MyNamespace.myFunction = function(arg1, arg2) {
// do things here
};
MyNamespace.myOtherFunction = function() {
// do other things here
};
Then, to call those functions, you would write `MyNamespace.myFunction(somearg, someotherarg);` and `MyNamespace.myOtherFunction();`.
I should also mention that there are many different ways to do namespacing and class-like things in Javascript. My method is just one of those many.
For more discussion, you might also want to take a look at this question.
Problem
I am bit confused with namespaces of function in javascript. Can I have function with same names? Thanks