That's similar to the Labeler proposed by Mark, except it needs to search
for the thing twice (wm.has + wm.get instead of just wm.get which would be
 just fine as check if you count from 1 instead of 0 ;-) )

Although I need to understand if you have a wm why wouldn't you just use
that instead of a Symbol ... so the Labeler seems again a more appropriate
pattern for this problem.

Just my thoughts

On Wed, Sep 9, 2015 at 10:51 AM, Michał Wadas <michalwa...@gmail.com> wrote:

> Following solution will work:
>
> (function(){
> const wm = new WeakMap();
> Symbol.identity = Symbol( 'Symbol.identity' );
> let i = 0;
> Object.defineProperty(
>   Object.prototype,
>   Symbol.identity,
>   {
>     get: function () {
>       if (wm.has(this)) return wm.get(this);
>       wm.set(this, Symbol(i++));
>       return wm.get(this);
>     }
>   }
> );
> })()
>
>
> 2015-09-08 23:29 GMT+02:00 Andrea Giammarchi <andrea.giammar...@gmail.com>
> :
>
>> as side note, that's just a lazy assignment that doesn't need two symbols
>> and a constant get invoke ...
>>
>> ```js
>> Symbol.identity = Symbol( 'Symbol.identity' );
>> var OBJECT_ID = 0;
>> Object.defineProperty(
>>   Object.prototype,
>>   Symbol.identity,
>>   {
>>     get: function () {
>>       // first time this is invoked ... and no more ...
>>       Object.defineProperty(this, Symbol.identity, {value: ++OBJECT_ID});
>>       // from now own, direct property access \m/
>>       return this[Symbol.identity];
>>     }
>>   }
>> );
>> ```
>>
>> Regards
>>
>> On Tue, Sep 8, 2015 at 9:57 PM, Michael McGlothlin <
>> mike.mcgloth...@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;
>>>    }
>>>    return this[ identity ];
>>>   }
>>>  } );
>>>
>>> On Tue, Sep 8, 2015 at 1:44 PM, Mark S. Miller <erig...@google.com>
>>> wrote:
>>>
>>>> See Labeler at
>>>> http://wiki.ecmascript.org/doku.php?id=harmony:weak_maps#unique_labeler
>>>>
>>>>
>>>>
>>>> On Tue, Sep 8, 2015 at 11:16 AM, joe <joe...@gmail.com> wrote:
>>>>
>>>>> Didn't send to list, something is wrong with my reply all. Sorry about
>>>>> that. Stupid mobile gmail.
>>>>> ---------- Forwarded message ----------
>>>>> From: "joe" <joe...@gmail.com>
>>>>> Date: Sep 8, 2015 11:15 AM
>>>>> Subject: Re: Object id, hash, etc?
>>>>> To: "Garrett Smith" <dhtmlkitc...@gmail.com>
>>>>> Cc:
>>>>>
>>>>> I agree with this request. This is the logical complement to valueof,
>>>>> I think. And yes, for most ID use cases this isn't a good fit, but we're
>>>>> not talking about the general case, just the cases where a python style
>>>>> id() function *is* appropriate.
>>>>>
>>>>> Joe
>>>>> On Sep 8, 2015 9:08 AM, "Garrett Smith" <dhtmlkitc...@gmail.com>
>>>>> wrote:
>>>>>
>>>>>> On 9/8/15, Michael McGlothlin <mike.mcgloth...@gmail.com> wrote:
>>>>>> > Is there a reason not to provide an object id and hash value as
>>>>>> other
>>>>>> > languages often do? I find myself defining an identity symbol on
>>>>>> objects
>>>>>> > with a value from a simple global counter. It makes it easier to
>>>>>> debug if I
>>>>>> > can just look at a log and see what objects and functions were
>>>>>> active.
>>>>>> >
>>>>>> > Likewise a hash value would simplify a lot of comparisons and make
>>>>>> it easier
>>>>>> > to debug.
>>>>>> >
>>>>>> > I especially find the ID useful when working with bound functions
>>>>>> as they
>>>>>> > can be difficult to tell apart. I like to change toString() to
>>>>>> provide the
>>>>>> > ID followed by the code (flattened to one line).
>>>>>> >
>>>>>> >
>>>>>>
>>>>>> NFE's are safe to use where IE8 support isn't needed*.
>>>>>> (function aa(){}).name; // "aa"
>>>>>>
>>>>>> As for using Object IDs and Object Pooling, lexically-scoped values
>>>>>> have benefits over global ID generators (as your counter). The values
>>>>>> can be passed in as a parameter to the Factory (useful with Private
>>>>>> Proxy). There other benefits to this pattern regarding memory
>>>>>> management and application design. Contact me if you'd like to learn
>>>>>> more.
>>>>>>
>>>>>> * https://kangax.github.io/nfe/
>>>>>> --
>>>>>> Garrett
>>>>>> @xkit
>>>>>> ChordCycles.wordpress.com
>>>>>> garretts.github.io
>>>>>> personx.tumblr.com
>>>>>> _______________________________________________
>>>>>> es-discuss mailing list
>>>>>> es-discuss@mozilla.org
>>>>>> https://mail.mozilla.org/listinfo/es-discuss
>>>>>>
>>>>>
>>>>> _______________________________________________
>>>>> es-discuss mailing list
>>>>> es-discuss@mozilla.org
>>>>> https://mail.mozilla.org/listinfo/es-discuss
>>>>>
>>>>>
>>>>
>>>>
>>>> --
>>>>     Cheers,
>>>>     --MarkM
>>>>
>>>> _______________________________________________
>>>> es-discuss mailing list
>>>> es-discuss@mozilla.org
>>>> https://mail.mozilla.org/listinfo/es-discuss
>>>>
>>>>
>>>
>>> _______________________________________________
>>> es-discuss mailing list
>>> es-discuss@mozilla.org
>>> https://mail.mozilla.org/listinfo/es-discuss
>>>
>>>
>>
>> _______________________________________________
>> es-discuss mailing list
>> es-discuss@mozilla.org
>> https://mail.mozilla.org/listinfo/es-discuss
>>
>>
>
_______________________________________________
es-discuss mailing list
es-discuss@mozilla.org
https://mail.mozilla.org/listinfo/es-discuss

Reply via email to