ToPropertyDescriptor and [[DefineOwnProperty]], regarding Object.defineProperty

Jeff Walden jwalden+es at MIT.EDU
Tue Jun 2 11:27:28 PDT 2009


On 1.6.09 13:47, Allen Wirfs-Brock wrote:
> The main reason we needed to deal with such partial property descriptors is to enable the use case of Object.defineProperty where someone wants to set the state of some specific attribute of an already existing property.  We wanted to enable a programmer to, for example, write:
> 	Object.defineProperty(myObj,"myProp", {configurable:false});
> Rather than:
> 	var desc= Object.getOwnPropertyDescriptor(myObj,"myProp");
> 	desc.configurable=false;
> 	Object.defineProperty(myObj,"myProp", desc);
>
> If we are going to allow such modification of individual attributes (or subsets of attributes) then we need to specify the acceptable attribute transformations for an existing property.  That is specified by the algorithm steps of [[DefineOwnProperty]]. The specific encoding use within internal property descriptors to represent the intention to change or to leave unmodified a particular attribute doesn't significantly change the complexity of [[DefineOwnProperty]]. In earlier drafts we actually experimented with using fully populated property descriptors and attribute fields with three possible values logic (true, false, unspecified).  This actually added some complexity to the specification (for example, we couldn't use the internal Boolean type to define the expected field values) so we decided to use the absence of attribute field to indicate that its value is not being explicitly specified.

It would be simpler to implement, less complicated to understand, and more naturally named if this only-twiddle-attributes functionality were its own method -- for example, Object.alterProperty or Object.modifyProperty or Brendan's Object.changeProperty.  For a rough specification, simply start with a property descriptor initialized from [[GetOwnProperty]], crib the steps of the current ToPropertyDescriptor that deal with the boolean fields of property descriptors to change attributes, then call [[DefineOwnProperty]] with the desired property and potentially-modified descriptor.  This would allow twiddling of individual bits, but it wouldn't add another possibility for the value of every field in a property descriptor.

Jeff


More information about the es-discuss mailing list