Nuking misleading properties in `Object.getOwnPropertyDescriptor`

Brandon Benvie bbenvie at mozilla.com
Thu Mar 14 09:01:38 PDT 2013


On 3/14/2013 2:12 AM, David Bruant wrote:
> Le 14/03/2013 08:51, Tom Van Cutsem a écrit :
>> [+Allen]
>>
>> 2013/3/13 Nathan Wall <nathan.wall at live.com 
>> <mailto:nathan.wall at live.com>>
>>
>>     However, as a matter of principle, my argument is that
>>     `Object.getOwnPropertyDescriptor` should, at the bare minimum,
>>     return a descriptor that can be known to work in
>>     `Object.defineProperty`.  If `Object.defineProperty` doesn't
>>     accept it, then you `getOwnPropertyDescriptor` didn't really give
>>     me a valid descriptor.
>>
>>     I think that this behavior (1) limits the creativity of
>>     developers to define properties like `Object.prototype.get`, (2)
>>     is a potential stumbling block, (3) has no real benefit --
>>     really, there's not anything positive about this behavior, and
>>     (4) forces developers who want to support `Object.prototype.get`
>>     to add an extra layer of cleaning before using `defineProperty`.
>>
>>
>> While the monkey-patching of Object.prototype ("don't do that!") is 
>> still the culprit, I agree that it would have been better if 
>> defineProperty looked only at "own" properties of the descriptor.
> In a previous message, Brandon Benvie mentioned he uses inheritance to 
> reuse a property descriptor [1] (I think there was another quote of 
> him, but I can't find it now). I can imagine it's a used pattern.

I also mentioned I thought it was unlikely to be commonly used, since 
I've never seen it used besides some of my own code (which exists in a 
couple libraries used by few or just me). I think, though, the other 
thing I mentioned that you're thinking of is that methods on the 
Descriptor class prototypes can be useful. Here's a simple version that 
demonstrates potential utility:


   const fields = new Set(['enumerable', 'configurable', 'writable', 
'value', 'get', 'set', 'key']);

   class Descriptor extends null {
     constructor(desc){
       if (desc) {
         for (let [key, value] of items(desc)) {
           if (fields.has(key) && value === undefined || 
Object.prototype[key] !== value) {
             this[key] = value;
           }
         }
       }
     }
     hide(){
       this.enumerable = false
     }
     lock(){
       this.configurable = false;
     }
     define(object, key = this.key){
       Object.defineProperty(object, key, this);
     }
     /** etc **/
   }

   class AccessorDescriptor extends Descriptor {
     setOn(object, value){
       if (typeof this.set === 'function') {
         return call(this.set, object, value) !== false;
       }
       return false;
     }
     getOn(object){
       if (typeof this.get === 'function') {
         return call(this.get, object);
       }
     }
     /** etc **/
   }
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20130314/a8a7be8d/attachment-0001.html>


More information about the es-discuss mailing list