Good day.
I am facing a rather simple trouble. I hope the community will help me.
Here’s what I have:
I have two JavaScript files: I have Parent class in the 1st, and Child class in the 2nd.
Now I’d like to hide these definitions from the global namespace. Of course, I could put them all into single JavaScript file and wrap it into a self-invoking anonymous function like this so that there will be only one instance of the child object (what I am trying to achieve):
var myChildObj = (function(){
var ParentClass = function() {};
ParentClass.prototype = {
//some data
};
function extendsClass(childClass, parentClass, isRewrite) {
for(var property in parentClass.prototype) {
if (isRewrite ||
(!isRewrite &&
!isDefined(childClass.prototype[property]))) {
childClass.prototype[property] = parentClass.prototype[property];
}
}
childClass.prototype.parent = parentClass.prototype;
}
var ChildClass = function() {};
ChildClass.prototype = {
//some data
}
extendsClass(ChildClass, ParentClass, false);
return new PhotoGallerySingle();
})();
But ParentClass declaration is rather huge and it will be not so programmers friendly to put it all in there.
So, how do you usually go about solving such problem? Thank you.
I’m afraid you can’t do that.
You could always attach it to some other object than the direct global namespace, but if you want to be able to use it in your second script, you need to make it public somewhere. Eventually, it will need to be accessible from the global object.
Sorry.