I´ve defined a class like this:
function Class1(){
this.Func1 = function(){
/* Methods and vars */
};
function Func2(){
/* Methods and vars */
};
};
I want to find out a way to call the public method (or get the value of a public variable) from the private one (Func2()). Any sugestions?
Pd: Sorry if the terminology I used is strongly oriented to objects, because I am a C++ programer, and I’m kinda newby in javascript programming.
From
Func1, you can callFunc2directly:However, you cannot do the same to call
Func1fromFunc2becauseFunc2will (probably) have a different scope and different definition ofthiswhen it is called;this.Func1will be undefined. As alx suggested below, you can save the scope using another variable that will retain its value when used from the inside function. You can also save a reference toFunc1in local scope as follows:This works because it does not rely on the changing reference
this.