Skip to content

Instantly share code, notes, and snippets.

@dan-cooke
Last active May 23, 2017 09:51
Show Gist options
  • Save dan-cooke/0301014c5562a25e602e3abfe7b69b77 to your computer and use it in GitHub Desktop.
Save dan-cooke/0301014c5562a25e602e3abfe7b69b77 to your computer and use it in GitHub Desktop.
JS defineProperty demo
'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