Is there a way to call \"public\" javascript functions from \"private\" ones within a class?
Check out the class below:
function Class()
{
this.p
The accepted answer has the possibly undesirable side effect that separate copies of publicMethod, test, and privateMethod will be created in each instance. The idiom for avoiding this is:
function Class()
{}
Class.prototype=(function()
{
var privateMethod = function(self)
{
self.publicMethod();
}
return
{
publicMethod: function()
{
alert("hello");
},
test: function()
{
privateMethod(this);
}
};
}());
In other words, you need to pass the this to the private function as an argument. In return, you get a true prototype without having to pollute each instance with its own versions of the private and public functions.