Here and here I already explained some ways of introducing methods in javascript, and now i will show one more way.
Let's say we have JS like:
(function (ns) {
function myCreator () {
var self=this;
function privateMethod() {
alert("Hi I am private method and I can be executed only within myCreator class (I will never be called)");
}
self.publicMethod = function () {
document.getElementById("publicMethodCall").innerHTML = "Hi, I am called from publicMethod";
}
}
ns.myCreator = myCreator;
}(window.privateAndPublic))
Then, we introduce JS like:
(function (ns) {
function myCreatorReturn() {
var self = this;
self.myCreator = new window.privateAndPublic.myCreator();
return {
myCreator: self.myCreator
}
}
ns.myCreatorReturn = myCreatorReturn;
}(window.myReturn))
Here notice line:
self.myCreator = new window.privateAndPublic.myCreator();
Now we can call method "publicMethod" like:
window.onload = function() {
mCR = new window.myReturn.myCreatorReturn();
mCR.myCreator.publicMethod();
}
Notice line:
mCR.myCreator.publicMethod();
Example download from here.