Last active
August 29, 2015 14:12
-
-
Save skeep/8e4d483bf8b0d1409a21 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 user = {}; | |
Object.defineProperty(user, 'age', { | |
enumerable: false, // will make age not appear in Object.keys(user) | |
configurable: false, // prevent deletion/ modification of the property and its value | |
writable: false, // will prevent changing of the property value | |
value: 30 | |
}); | |
Object.keys(user); | |
// return [] | |
Object.getOwnPropertyNames(user); | |
// return ["age"] | |
user.age = 25; | |
// will throw "TypeError: Cannot assign to read only property 'age' of #<Object>" in strict mode | |
console.log(user.age); | |
// will log 30 as the property is not writable | |
delete user.age | |
// returns false |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment