Created
November 19, 2014 12:34
-
-
Save shaunwallace/a4e65d8b526bd4a284a9 to your computer and use it in GitHub Desktop.
Object.defineProperty()
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 obj4 = {}; // create a new empty object | |
| Object.defineProperty(obj4, 'x', { | |
| value : 'Some generic string', | |
| writable : true, | |
| enumerable : true, | |
| configurable : true | |
| }); | |
| obj4.x; // returns "Some generic string" | |
| Object.keys( obj4 ); // returns ["x"] | |
| obj4.x = 'Another generic test string'; | |
| obj4.x; // returns "Another generic test string" | |
| // since we have set configurable to true we can alter the property and its | |
| // attributes can be changed. One thing to note is that if this property was set to false then trying to | |
| // make updates to it other than changing the writable property will fail and attempting to delete a property will also fail | |
| Object.defineProperty(obj4, 'x', { | |
| value : 'And again some generic string', | |
| writable : true, | |
| enumerable : false, | |
| configurable : false | |
| }); | |
| obj4.x; // returns "And again some generic string" | |
| Object.keys( obj4 ); // returns [] | |
| // this will result in a TypeError because we cannot redefine the property: x once configurable has been set to false | |
| Object.defineProperty(obj4, 'x', { | |
| value : 'And again some generic string', | |
| configurable : true | |
| }); | |
| // we can however set the writable property once the configurable property has been set to false | |
| Object.defineProperty(obj4, 'x', { | |
| writable : false | |
| }); | |
| obj4.x = 'A string that wont be set'; | |
| obj4.x; // returns "And again some generic string" since we can no longer write to it | |
| // adding properties and default values | |
| // its important to note the following: | |
| var obj5 = { | |
| x : true | |
| }; | |
| // is equivalent to: | |
| Object.defineProperty(obj5, 'x', { | |
| value : true, | |
| writable : true, | |
| configurable : true, | |
| enumerable : true | |
| }); | |
| // but... | |
| Object.defineProperty(obj6, 'x', { value : true }); | |
| // is equivalent to: | |
| Object.defineProperty(obj6, 'x', { | |
| value : true, | |
| writable : false, | |
| configurable : false, | |
| enumerable : false | |
| }); | |
| // Example of using defineProperty with an accessor property descriptor which can be useful when you | |
| // want to execute some code each time a property is set or retrieved | |
| var obj7 = {} | |
| , y = 100; | |
| Object.defineProperty(obj7, 'y', { | |
| get : function() { | |
| console.log('getting property'); | |
| return y; | |
| }, | |
| set : function( value ) { | |
| console.log('setting property'); | |
| y = value; | |
| }, | |
| enumerable : true, | |
| configurable: true | |
| }); | |
| obj7.y; // returns |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment