Mascara: Next Generation JavaScript compiler

Try it Download Buy About Documentation Blog Contact

Namespaces

Namespaces are used to disambiguate variable names. The same variable name can be used in different namespaces.

Defining a namespace:

namespace superduper;
Defining a variable in a namespace:
superduper var x = 100;

The variable can be accessed explicitly by prefixing with the namespace followed by double colon ::

print(superduper::x); // prints 100

Namespaces can be imported which means that all variables in the namespace will be made available without the need to prefix with the namespace:

use namespace superduper;
print(x); // also works - prints 100

The one caveat is (of course that) if you have the same name defined in different namespaces which are imported at the same time, the name becomes ambiguous if used without the prefix:

 // create another namespace
namespace slaraffenland;
slaraffenland var x = 72; // define a different x
 
use namespace slaraffenland;
use namespace superduper;
 
print(slaraffenland::x); // prints 72
print(superduper::x);  // prints 100
 
print(x); // compile error! x is ambiguous!
X Try!
Mascara JavaScript Generated JavaScript

Translating...
Show compiler settings