JavaScript Mapping for Constants

Here are the sample constant definitions once more:

Slice
module Example {
const bool      AppendByDefault = true;
const byte      LowerNibble = 0x0f;
const string    Advice = "Don't Panic!";
const short     TheAnswer = 42;
const double    PI = 3.1416;

enum Fruit { Apple, Pear, Orange };
const Fruit     FavoriteFruit = Pear;
};

For each constant, the JavaScript mapping generates a read-only property of the same name in the enclosing scope as shown below:

JavaScript
Object.defineProperty(Example, 'AppendByDefault', {value: true});
Object.defineProperty(Example, 'LowerNibble', {value: 15});
Object.defineProperty(Example, 'Advice', {value: "Don't Panic!"});
Object.defineProperty(Example, 'TheAnswer', {value: 42});
Object.defineProperty(Example, 'PI', {value: 3.1416});
Object.defineProperty(Example, 'FavoriteFruit', {value: Fruit.Pear});

Slice string literals that contain non-ASCII characters or universal character names are mapped to JavaScript string literals with universal character names. For example:

Slice
const string Egg = "œuf";
const string Heart = "c\u0153ur";
const string Banana = "\U0001F34C";

is mapped to:

JavaScript
Object.defineProperty(Example, 'Egg', {value: "\u0153uf"});
Object.defineProperty(Example 'Heart', {value: "c\u0153ur"});
Object.defineProperty(Example, 'Banana', {value: "\ud83c\udf4c"});

See Also