Skip to content

Instantly share code, notes, and snippets.

@skeep
Last active August 29, 2015 14:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save skeep/8e4d483bf8b0d1409a21 to your computer and use it in GitHub Desktop.
Save skeep/8e4d483bf8b0d1409a21 to your computer and use it in GitHub Desktop.
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