Mascara: Next Generation JavaScript compiler

Try it Download Buy About Documentation Blog Contact

Access modifiers

Class members and package members can have access modifiers public or private.

Syntax definition [Help]
[ private | public ] [ var | const ] ...
[ private | public ] function ...

class A {
    public var a = 10; // public variable
    private function secret() {
       return "abracadabra";
    }
}

A private member is accessible only by methods in the same class. A public member is accessibly from everywhere.

Members are public by default.

Static members can also be have access modifiers. 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.)

X Try!
Mascara JavaScript Generated JavaScript

Translating...
Show compiler settings