Hi! This blog is for news, announcements and questions regarding the
Mascara JavaScript compiler.

See also:
The online demo
Download latest release

Contact:
olav@olav.dk

Disclaimer:
ECMAScript is a trademark of Ecma International.

Powered by Squarespace
« Parser error at or near '{' | Main | Python 2.6 builds »
Thursday
Sep102009

Access modifiers in Mascara

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!

PrintView Printer Friendly Version

EmailEmail Article to Friend