Thursday, July 23, 2015

Object.defineProperty

Yet another goodie revealed to me by Kyle Simpson's book "this & OBJECT PROTOTYPES" is Object.defineProperty. You may use it to set the writable, configurable, and enumerable settings on a property like so:

var thingy = {};
Object.defineProperty(thingy, "whatever", {
   value: 13,
   writable: true,
   configurable: true,
   enumerable: true
});
alert(thingy.whatever);

 
 

Setting them all to true however gets you nothing in verbosely using the Object.defineProperty as these are all the default states anyways for these settings. writable as false blocks reassignment while enumerable as false is going to hinder you from looping through the property's guts. configurable of false keeps you from changing these settings a second time with the exception of yet being able to flip writable from true to false, that one, one-way change is still accessible for some reason. If writable and configurable are both false the thing at hand is effectively a constant.

No comments:

Post a Comment