/ Published in: JavaScript

Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
var myObject = {}; //wrapping Object.defineProperty in a function like this feels very hacky (function() { var myProp = 'myDefault'; Object.defineProperty( myObject, 'myProp', { enumerable: false, configurable: true, get: function() { return myProp; }, set: function( v ) { myProp = v+' is mo beta'; } }); })(); alert( myObject.myProp ); myObject.myProp = 'myValue'; alert( myObject.myProp ); /* COULDN'T IT HAVE JUST BEEN.... Object.defineProperty( myObject, 'myProp', { writable: false, enumerable: false, configurable: true, value: 'myDefault', get: function() { return myProp; }, set: function( v ) { myProp = v+' is mo beta'; } }); */
Comments
