This is a very early stage 0 exploration to add enums to Javascript, as a syntatic simplication over a common pattern to define enumerations.
Enums come up often in code bases (TODO: try to estimate a number) and it is easy to get it incorrectly. Specifically, it is easy to forget:
- to
Object.freezethe object - to declare it as a
constwhich avoids having the symbol redefined
With this in mind, we propose a nem keyword to javascript, say enum which de-sugars to the following:
// if you write this ...
enum Foo {
BAR: 1,
HELLO "hello",
WORLD: false
}
// ... it gets de-sugared to:
const Foo = Objec.freeze({
BAR: 1,
HELLO: "hello",
WORLD: false
});
// TODO(goto): should we use Symbols here for extra type safety?
Examples
https://github.com/jspears/subschema/blob/master/src/listenUtil.js#L144
https://github.com/WaffleBoardWizard/guild-ball-simulator/blob/04ae96c7561e7b9c3c0959134a34f6a6c3708a58/gb-simulator-web/src/components/Controls/GameBoard/Inputs.js#L1
https://github.com/Piicksarn/cdnjs/blob/master/ajax/libs/Shuffle/2.0.6/jquery.shuffle.js#L103
jquery
angular
https://github.com/angular/bower-material/blob/master/modules/js/panel/panel.js#L2504
https://github.com/mirror/chromium/blob/master/chrome/browser/resources/settings/site_settings/constants.js
Azure
https://github.com/Azure/azure-storage-node/blob/master/lib/common/util/constants.js