Created
August 26, 2013 19:45
-
-
Save blainsmith/6345780 to your computer and use it in GitHub Desktop.
JavaScript Constant Variables with Object.freeze();
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
var CONST = { | |
API_KEY: '123456789abcdefg', | |
PI: 3.14 | |
}; | |
Object.freeze(CONST); | |
// Will print {API_KEY: "123456789abcdefg", PI: 3.14} | |
console.log(CONST); | |
// Attempt to change the value of PI to something else, but won't happen | |
CONST.PI = 2.2399; | |
// Attempt to add a new key to to the object | |
CONST.NEW_CONST = 'test'; | |
// Will print the same {API_KEY: "123456789abcdefg", PI: 3.14} | |
console.log(CONST); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment