Access modifiers in Mascara
Thursday, September 10, 2009 at 05:39PM Access modifiers (private and public) are among the most frequently requested features. They are now supported in the latest Mascara version, 1.2.4 (Download).
A private member is accessible only by methods in the same class. A public member is accessibly by everyone, just as members always are in classic JavaScript.
class A {
public function famous() {};
private function secret() {};
private function test() {
famous(); //OK because famous is public
secret(); //OK because we are members of the same class
}
}
var a = new A();
a. famous(); //OK
a. secret(); //ERROR, secret is not accessible from here
Members are public by default.
Static members can also be private. Private static methods can access private instance members in the same class and vice versa.
Constructors can also be private. With a private constructor it is not possible for other classes to create instances of the class. Static methods on the same class are then used to instantiate and return instances of the class. This can be used to implement patterns like singleton and factory.
Have fun with encapsulation!
Olav | Comments Off | 