Last active
May 23, 2017 09:51
-
-
Save dan-cooke/0301014c5562a25e602e3abfe7b69b77 to your computer and use it in GitHub Desktop.
JS defineProperty demo
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
'use strict'; | |
var t = { | |
mutableProp: 'helloWorld' | |
}; | |
//by default Object.defineProperty creates an immutable property that is not enumerable | |
Object.defineProperty(t, 'immutableProp', { | |
value: 'worldHello!', | |
configurable: true | |
}); | |
console.log('T with non enumerable immutable prop ', t); | |
console.log('-------------'); | |
Object.defineProperty(t, 'immutableProp', { | |
enumerable: true | |
}); | |
console.log('T with enumerable immutable prop ', t); | |
console.log('-------------'); | |
Object.defineProperty(t, 'immutableProp', { | |
configurable: false, | |
writable: false, | |
value: 'FROZEN PROPERTY', | |
enumerable: true | |
}); | |
console.log('T frozen immutable prop ', t); | |
console.log('-------------'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment