Do we really need the [[HasOwnProperty]] internal method and hasOwn trap

Herby Vojčík herby at mailbox.sk
Tue Nov 13 00:10:49 PST 2012



Brandon Benvie wrote:
> A better trap than getOwnPropertyDescriptor would be something like
> "query" which is used to look up the attributes of a property and its
> existence.  JS shies away from constants generally, and especially
> numeric ones, but this is a place I have found really benefits from a
> bitfield.
>
> const ENUMERABLE = 0x01,
>        CONFIGURABLE = 0x02,
>        WRITABLE = 0x04
>        ACCESSOR = 0x08;
>
> const E__ = 1,
>        _C_ = 2,
>        EC_ = 3,
>        __W = 4,
>        E_W = 5,
>        _CW = 6,
>        ECW = 7,
>        ___A = 8,
>        E__A = 9,
>        _CA = 10,
>        ECA = 11;
> function query(target, property){
>    // returns a value corresponding to the own property attributes, or
> undefined if no own property
> }

problem is, there can be ___=0; which will evaluate to false.
It would benefit from
         THEREISADESCRIPTOR = 0x10;

and all your enums from above being 16 higher.

And btw, no need for predefined consts, you can simply 
Reflect.flagsOf({enumerable:true, value:5}) returning 17, for example.
This result you can (and should) const for your later use, of course.

> "has" is removed in favor of hasOwn and proto walking
> "hasOwn" is then replaced with query, where undefined means false and
> any value corresponding to a flag is true
> "getOwnPropertyDescriptor" becomes a "query" followed by "get" or "set"
> if the query result isn't undefined
>
>
> The one loss is this prevents custom descriptor attributes. That seems

It does not, why? If the descriptor is there, get returns them. You 
meant "custom queriable attributes"?

In the case of Reflect.flagsOf, it does not ;-)

> like a fringe use case to me though. The benefit is the removal of all
> the traffic back and forth allocating objects and internal descriptors.
> It also reduces the number of traps and separates the concerns of
> property values from property attributes.

Herby


More information about the es-discuss mailing list