Is there a way to call "public" javascript functions from "private" ones within a class?
Check out the class below:
function Class()
{
this.publicMethod = function()
{
alert("hello");
}
privateMethod = function()
{
publicMethod();
}
this.test = function()
{
privateMethod();
}
}
Here is the code I run:
var class = new Class();
class.test();
Firebug gives this error:
publicMethod is not defined: [Break on this error] publicMethod();
Is there some other way to call publicMethod() within privateMethod() without accessing the global class variable [i.e. class.publicMethod()]?
You can save off a variable in the scope of the constructor to hold a reference to
this
.Please Note: In your example, you left out
var
beforeprivateMethod = function()
making thatprivateMethod
global. I have updated the solution here:The accepted answer has the possibly undesirable side effect that separate copies of
publicMethod
,test
, andprivateMethod
will be created in each instance. The idiom for avoiding this is: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.Is this approach not a advisable one? I am not sure though
torazaburo's answer is the best one, as it avoids creation of multiple copies of the private members. I'm surprised that Crockford doesn't mention it at all. Alternately, depending on the syntax you prefer for declaring public member functions, you could do this: