I have a namespacein my JavaScript code “MyNameSpace”. I am creating a static JavaScript class “MyChildStaticClass” in it. I am using Modular pattern. Below is my code:
if (typeof MyNameSpace == 'undefined' || !MyNameSpace) {
var MyNameSpace = {};
}
var MyNameSpace = (function(MyNameSpace) {
MyNameSpace.MyChildStaticClass = (function() {
var myobject;
myobject = {
x:function(str) {
alert(str);
}
};
return myobject;
})();
return MyNameSpace;
} (MyNameSpace || {}));
The above code will be used like:
MyNameSpace.MyChildStaticClass.x('test');
and the output of above will be an alert box with message test. I have the question that is this a good way of creating a static class and calling methods like the above? Is there any other ways to write it in proficient manner?
You would get the same functionality by doing: