How can I create a function that can’t be called from outside?
var obj = {
function1: function(){
alert("function1");
},
function2: function(){
alert("function2...");
obj.function1();
}
};
// so how to make this function unaccessible
obj.function1();
// and you could only call this function
obj.function2();
You may want to consider using the Yahoo Module Pattern. This is a singleton pattern, and the methods are not really static, but it may be what you are looking for:
You define your private members where
myPrivateVarandmyPrivateMethodare defined, and your public members wheremyPublicVarandmyPublicMethodare defined.You can simply access the public methods and properties as follows: