Object id, hash, etc?

Tab Atkins Jr. jackalmage at gmail.com
Tue Sep 8 21:12:45 UTC 2015


On Tue, Sep 8, 2015 at 2:07 PM, Mark S. Miller <erights at google.com> wrote:
> On Tue, Sep 8, 2015 at 1:57 PM, Michael McGlothlin
> <mike.mcglothlin at gmail.com> wrote:
>>
>> I try to keep it pretty simple. It's not fancy but the times you want fast
>> and dirty information like this are the same times you don't want to have to
>> define it manually.
>>
>>  Symbol.identity = Symbol( 'Symbol.identity' );
>>  const identity = Symbol( 'identity' );
>>  var OBJECT_ID = 0;
>>  Object.defineProperty( Object.prototype, Symbol.identity, {
>>   get: () => {
>>    if ( !Object.hasOwnProperty.call( this, identity ) ) {
>>     this[ identity ] = ++OBJECT_ID;
>
>
> Does not work on frozen objects.

Of course, it's trivial to switch it to WeakMap'ing the key.

~TJ


More information about the es-discuss mailing list