Last active
August 29, 2015 14:18
-
-
Save mrspeaker/ce1cc886c8324e9b8bd2 to your computer and use it in GitHub Desktop.
JS immutable identifier binding
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
A sweet.js macro for adding a JS identifier | |
binding (`val`) that is the same as a `const` declaration, | |
that is then frozen with `Object.freeze`. | |
*/ | |
'use strict'; | |
macro val { | |
rule { | |
$Identifier:ident = $Initializer:expr; | |
} => { | |
const $Identifier = Object.freeze($Initializer); | |
} | |
} | |
val a = [1,2,3,4]; | |
val b = { test: "yep" }; | |
a.push(5); | |
>> TypeError: a.push(...) is not extensible | |
b.test = "nope"; | |
>> TypeError: "test" is read-only |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is a sweet.js macro. Test it out in the editor.