Object id, hash, etc?
Changing toString() is obviously not possible since the semantics are fixed and many existing code depends them (ex. Object.prototype.toString are used for type identifying, String.prototype.toString are used for meta-programming frameworks. I think you can extend Object.prototype to provide a hashing method if you really need them for debugging. Personally I think it is not usually needed.
Changing toString() is obviously not possible since the semantics are fixed and many existing code depends them (ex. Object.prototype.toString are used for type identifying, String.prototype.toString are used for meta-programming frameworks. I think you can extend Object.prototype to provide a hashing method if you really need them for debugging. Personally I think it is not usually needed. -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20150908/bfbf36fc/attachment.html>
On 9/8/15, Michael McGlothlin <mike.mcglothlin at 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.
On 9/8/15, Michael McGlothlin <mike.mcglothlin at 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
Didn't send to list, something is wrong with my reply all. Sorry about that. Stupid mobile gmail. ---------- Forwarded message ---------- From: "joe" <joeedh at gmail.com>
Date: Sep 8, 2015 11:15 AM Subject: Re: Object id, hash, etc? To: "Garrett Smith" <dhtmlkitchen at 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
Didn't send to list, something is wrong with my reply all. Sorry about that. Stupid mobile gmail. ---------- Forwarded message ---------- From: "joe" <joeedh at gmail.com> Date: Sep 8, 2015 11:15 AM Subject: Re: Object id, hash, etc? To: "Garrett Smith" <dhtmlkitchen at 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" <dhtmlkitchen at gmail.com> wrote: > On 9/8/15, Michael McGlothlin <mike.mcglothlin at 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 at mozilla.org > https://mail.mozilla.org/listinfo/es-discuss > -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20150908/d809eaa0/attachment.html>
See Labeler at harmony:weak_maps#unique_labeler
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 <joeedh at 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" <joeedh at gmail.com> > Date: Sep 8, 2015 11:15 AM > Subject: Re: Object id, hash, etc? > To: "Garrett Smith" <dhtmlkitchen at 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" <dhtmlkitchen at gmail.com> wrote: > >> On 9/8/15, Michael McGlothlin <mike.mcglothlin at 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 at mozilla.org >> https://mail.mozilla.org/listinfo/es-discuss >> > > _______________________________________________ > es-discuss mailing list > es-discuss at mozilla.org > https://mail.mozilla.org/listinfo/es-discuss > > -- Cheers, --MarkM -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20150908/db0b815e/attachment-0001.html>
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 ]; } } );
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 <erights at 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 <joeedh at 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" <joeedh at gmail.com> >> Date: Sep 8, 2015 11:15 AM >> Subject: Re: Object id, hash, etc? >> To: "Garrett Smith" <dhtmlkitchen at 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" <dhtmlkitchen at gmail.com> wrote: >> >>> On 9/8/15, Michael McGlothlin <mike.mcglothlin at 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 at mozilla.org >>> https://mail.mozilla.org/listinfo/es-discuss >>> >> >> _______________________________________________ >> es-discuss mailing list >> es-discuss at mozilla.org >> https://mail.mozilla.org/listinfo/es-discuss >> >> > > > -- > Cheers, > --MarkM > > _______________________________________________ > es-discuss mailing list > es-discuss at mozilla.org > https://mail.mozilla.org/listinfo/es-discuss > > -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20150908/15c6feff/attachment.html>
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.
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. > } > return this[ identity ]; > } > } ); > > On Tue, Sep 8, 2015 at 1:44 PM, Mark S. Miller <erights at google.com> wrote: > >> See Labeler at >> http://wiki.ecmascript.org/doku.php?id=harmony:weak_maps#unique_labeler >> >> >> -- Cheers, --MarkM -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20150908/3cb9aa9f/attachment.html>
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.
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
then you get the Labeler, which this message is responding to.
Hi Tab, then you get the Labeler, which this message is responding to. On Tue, Sep 8, 2015 at 2:12 PM, Tab Atkins Jr. <jackalmage at gmail.com> wrote: > 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 > -- Cheers, --MarkM -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20150908/06e27f39/attachment.html>
as side note, that's just a lazy assignment that doesn't need two symbols and a constant get invoke ...
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];
}
}
);
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.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; > } > return this[ identity ]; > } > } ); > > On Tue, Sep 8, 2015 at 1:44 PM, Mark S. Miller <erights at 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 <joeedh at 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" <joeedh at gmail.com> >>> Date: Sep 8, 2015 11:15 AM >>> Subject: Re: Object id, hash, etc? >>> To: "Garrett Smith" <dhtmlkitchen at 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" <dhtmlkitchen at gmail.com> wrote: >>> >>>> On 9/8/15, Michael McGlothlin <mike.mcglothlin at 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 at mozilla.org >>>> https://mail.mozilla.org/listinfo/es-discuss >>>> >>> >>> _______________________________________________ >>> es-discuss mailing list >>> es-discuss at mozilla.org >>> https://mail.mozilla.org/listinfo/es-discuss >>> >>> >> >> >> -- >> Cheers, >> --MarkM >> >> _______________________________________________ >> es-discuss mailing list >> es-discuss at mozilla.org >> https://mail.mozilla.org/listinfo/es-discuss >> >> > > _______________________________________________ > es-discuss mailing list > es-discuss at mozilla.org > https://mail.mozilla.org/listinfo/es-discuss > > -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20150908/add4cb74/attachment.html>
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.giammarchi at gmail.com>:
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.giammarchi at 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.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; >> } >> return this[ identity ]; >> } >> } ); >> >> On Tue, Sep 8, 2015 at 1:44 PM, Mark S. Miller <erights at 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 <joeedh at 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" <joeedh at gmail.com> >>>> Date: Sep 8, 2015 11:15 AM >>>> Subject: Re: Object id, hash, etc? >>>> To: "Garrett Smith" <dhtmlkitchen at 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" <dhtmlkitchen at gmail.com> wrote: >>>> >>>>> On 9/8/15, Michael McGlothlin <mike.mcglothlin at 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 at mozilla.org >>>>> https://mail.mozilla.org/listinfo/es-discuss >>>>> >>>> >>>> _______________________________________________ >>>> es-discuss mailing list >>>> es-discuss at mozilla.org >>>> https://mail.mozilla.org/listinfo/es-discuss >>>> >>>> >>> >>> >>> -- >>> Cheers, >>> --MarkM >>> >>> _______________________________________________ >>> es-discuss mailing list >>> es-discuss at mozilla.org >>> https://mail.mozilla.org/listinfo/es-discuss >>> >>> >> >> _______________________________________________ >> es-discuss mailing list >> es-discuss at mozilla.org >> https://mail.mozilla.org/listinfo/es-discuss >> >> > > _______________________________________________ > es-discuss mailing list > es-discuss at mozilla.org > https://mail.mozilla.org/listinfo/es-discuss > > -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20150909/80324756/attachment-0001.html>
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
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 <michalwadas at 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.giammarchi at 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.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; >>> } >>> return this[ identity ]; >>> } >>> } ); >>> >>> On Tue, Sep 8, 2015 at 1:44 PM, Mark S. Miller <erights at 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 <joeedh at 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" <joeedh at gmail.com> >>>>> Date: Sep 8, 2015 11:15 AM >>>>> Subject: Re: Object id, hash, etc? >>>>> To: "Garrett Smith" <dhtmlkitchen at 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" <dhtmlkitchen at gmail.com> >>>>> wrote: >>>>> >>>>>> On 9/8/15, Michael McGlothlin <mike.mcglothlin at 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 at mozilla.org >>>>>> https://mail.mozilla.org/listinfo/es-discuss >>>>>> >>>>> >>>>> _______________________________________________ >>>>> es-discuss mailing list >>>>> es-discuss at mozilla.org >>>>> https://mail.mozilla.org/listinfo/es-discuss >>>>> >>>>> >>>> >>>> >>>> -- >>>> Cheers, >>>> --MarkM >>>> >>>> _______________________________________________ >>>> es-discuss mailing list >>>> es-discuss at mozilla.org >>>> https://mail.mozilla.org/listinfo/es-discuss >>>> >>>> >>> >>> _______________________________________________ >>> es-discuss mailing list >>> es-discuss at mozilla.org >>> https://mail.mozilla.org/listinfo/es-discuss >>> >>> >> >> _______________________________________________ >> es-discuss mailing list >> es-discuss at mozilla.org >> https://mail.mozilla.org/listinfo/es-discuss >> >> > -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20150909/47f14010/attachment.html>
Using frozen objects and such seems a lot more complex to me than just using a property. Using a symbol to hide the property from cluttering seems to go along with how other things are being done. And it's my understanding that other than hiding it that using a Symbol for a name is nothing special - does it have some negative factor that should make using it less attractive?
📱 Michael McGlothlin
Using frozen objects and such seems a lot more complex to me than just using a property. Using a symbol to hide the property from cluttering seems to go along with how other things are being done. And it's my understanding that other than hiding it that using a Symbol for a name is nothing special - does it have some negative factor that should make using it less attractive? 📱 Michael McGlothlin > On Sep 9, 2015, at 5:36 AM, Andrea Giammarchi <andrea.giammarchi at gmail.com> wrote: > > 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 <michalwadas at 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.giammarchi at 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.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; >>>> } >>>> return this[ identity ]; >>>> } >>>> } ); >>>> >>>>> On Tue, Sep 8, 2015 at 1:44 PM, Mark S. Miller <erights at 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 <joeedh at 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" <joeedh at gmail.com> >>>>>> Date: Sep 8, 2015 11:15 AM >>>>>> Subject: Re: Object id, hash, etc? >>>>>> To: "Garrett Smith" <dhtmlkitchen at 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" <dhtmlkitchen at gmail.com> wrote: >>>>>>> On 9/8/15, Michael McGlothlin <mike.mcglothlin at 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 at mozilla.org >>>>>>> https://mail.mozilla.org/listinfo/es-discuss >>>>>> >>>>>> _______________________________________________ >>>>>> es-discuss mailing list >>>>>> es-discuss at mozilla.org >>>>>> https://mail.mozilla.org/listinfo/es-discuss >>>>> >>>>> >>>>> >>>>> -- >>>>> Cheers, >>>>> --MarkM >>>>> >>>>> _______________________________________________ >>>>> es-discuss mailing list >>>>> es-discuss at mozilla.org >>>>> https://mail.mozilla.org/listinfo/es-discuss >>>> >>>> >>>> _______________________________________________ >>>> es-discuss mailing list >>>> es-discuss at mozilla.org >>>> https://mail.mozilla.org/listinfo/es-discuss >>> >>> >>> _______________________________________________ >>> es-discuss mailing list >>> es-discuss at mozilla.org >>> https://mail.mozilla.org/listinfo/es-discuss > -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20150909/d3089d24/attachment.html>
Well, to start with, you don't hide symbols, these are all exposed through
Object.getOwnPropertySymbols
or Reflect.ownKeys so for your use case
looks like using just a non enumerable property would do as well.
And btw, even if Symbols don't show up in for/in and Object.keys, these are
by default also enumerable so Object.assign will copy them around (reason
I've used defineProperty with the value instead of just setting the symbol,
so it's non enumerable by default).
As summary: do you want to be sure that object has a unique "lable" or "id" associated with it? Use WeakMaps, otherwise be prepared to possible clones around if some library uses Object.assign thinking that's a good way to copy properties around.
I still suggest the Labeler approach that Mark previously indicated.
Well, to start with, you don't hide symbols, these are all exposed through `Object.getOwnPropertySymbols` or Reflect.ownKeys so for your use case looks like using just a non enumerable property would do as well. And btw, even if Symbols don't show up in for/in and Object.keys, these are by default also enumerable so Object.assign will copy them around (reason I've used defineProperty with the value instead of just setting the symbol, so it's non enumerable by default). As summary: do you want to be sure that object has a unique "lable" or "id" associated with it? Use WeakMaps, otherwise be prepared to possible clones around if some library uses Object.assign thinking that's a good way to copy properties around. I still suggest the Labeler approach that Mark previously indicated. Regards On Wed, Sep 9, 2015 at 1:15 PM, Michael McGlothlin < mike.mcglothlin at gmail.com> wrote: > Using frozen objects and such seems a lot more complex to me than just > using a property. Using a symbol to hide the property from cluttering seems > to go along with how other things are being done. And it's my understanding > that other than hiding it that using a Symbol for a name is nothing special > - does it have some negative factor that should make using it less > attractive? > > 📱 Michael McGlothlin > > On Sep 9, 2015, at 5:36 AM, Andrea Giammarchi <andrea.giammarchi at gmail.com> > wrote: > > 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 <michalwadas at 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.giammarchi at 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.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; >>>> } >>>> return this[ identity ]; >>>> } >>>> } ); >>>> >>>> On Tue, Sep 8, 2015 at 1:44 PM, Mark S. Miller <erights at 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 <joeedh at 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" <joeedh at gmail.com> >>>>>> Date: Sep 8, 2015 11:15 AM >>>>>> Subject: Re: Object id, hash, etc? >>>>>> To: "Garrett Smith" <dhtmlkitchen at 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" <dhtmlkitchen at gmail.com> >>>>>> wrote: >>>>>> >>>>>>> On 9/8/15, Michael McGlothlin <mike.mcglothlin at 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 at mozilla.org >>>>>>> https://mail.mozilla.org/listinfo/es-discuss >>>>>>> >>>>>> >>>>>> _______________________________________________ >>>>>> es-discuss mailing list >>>>>> es-discuss at mozilla.org >>>>>> https://mail.mozilla.org/listinfo/es-discuss >>>>>> >>>>>> >>>>> >>>>> >>>>> -- >>>>> Cheers, >>>>> --MarkM >>>>> >>>>> _______________________________________________ >>>>> es-discuss mailing list >>>>> es-discuss at mozilla.org >>>>> https://mail.mozilla.org/listinfo/es-discuss >>>>> >>>>> >>>> >>>> _______________________________________________ >>>> es-discuss mailing list >>>> es-discuss at mozilla.org >>>> https://mail.mozilla.org/listinfo/es-discuss >>>> >>>> >>> >>> _______________________________________________ >>> es-discuss mailing list >>> es-discuss at mozilla.org >>> https://mail.mozilla.org/listinfo/es-discuss >>> >>> >> > -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20150909/acd2b884/attachment-0001.html>
I think using WeakMaps is a good idea. Using a Symbol will keep from cluttering the namespace with names others might want to use (id,label,etc) and seems more consistent than some external means of accessing the id. When I tried defining the value before I ran into issues with the prototype chain which is why I was using get and a second 'hidden' property. Using WeakMap seems like it'd fix the issue.
Can't say I've used Object.assign much - will have to experiment with it.
Thanks, Michael McGlothlin Sent from my iPhone
I think using WeakMaps is a good idea. Using a Symbol will keep from cluttering the namespace with names others might want to use (id,label,etc) and seems more consistent than some external means of accessing the id. When I tried defining the value before I ran into issues with the prototype chain which is why I was using get and a second 'hidden' property. Using WeakMap seems like it'd fix the issue. Can't say I've used Object.assign much - will have to experiment with it. Thanks, Michael McGlothlin Sent from my iPhone > On Sep 9, 2015, at 7:34 AM, Andrea Giammarchi <andrea.giammarchi at gmail.com> wrote: > > Well, to start with, you don't hide symbols, these are all exposed through `Object.getOwnPropertySymbols` or Reflect.ownKeys so for your use case looks like using just a non enumerable property would do as well. > And btw, even if Symbols don't show up in for/in and Object.keys, these are by default also enumerable so Object.assign will copy them around (reason I've used defineProperty with the value instead of just setting the symbol, so it's non enumerable by default). > > As summary: do you want to be sure that object has a unique "lable" or "id" associated with it? Use WeakMaps, otherwise be prepared to possible clones around if some library uses Object.assign thinking that's a good way to copy properties around. > > I still suggest the Labeler approach that Mark previously indicated. > > Regards > > > > >> On Wed, Sep 9, 2015 at 1:15 PM, Michael McGlothlin <mike.mcglothlin at gmail.com> wrote: >> Using frozen objects and such seems a lot more complex to me than just using a property. Using a symbol to hide the property from cluttering seems to go along with how other things are being done. And it's my understanding that other than hiding it that using a Symbol for a name is nothing special - does it have some negative factor that should make using it less attractive? >> >> 📱 Michael McGlothlin >> >>> On Sep 9, 2015, at 5:36 AM, Andrea Giammarchi <andrea.giammarchi at gmail.com> wrote: >>> >>> 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 <michalwadas at 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.giammarchi at 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.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; >>>>>> } >>>>>> return this[ identity ]; >>>>>> } >>>>>> } ); >>>>>> >>>>>>> On Tue, Sep 8, 2015 at 1:44 PM, Mark S. Miller <erights at 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 <joeedh at 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" <joeedh at gmail.com> >>>>>>>> Date: Sep 8, 2015 11:15 AM >>>>>>>> Subject: Re: Object id, hash, etc? >>>>>>>> To: "Garrett Smith" <dhtmlkitchen at 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" <dhtmlkitchen at gmail.com> wrote: >>>>>>>>> On 9/8/15, Michael McGlothlin <mike.mcglothlin at 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 at mozilla.org >>>>>>>>> https://mail.mozilla.org/listinfo/es-discuss >>>>>>>> >>>>>>>> _______________________________________________ >>>>>>>> es-discuss mailing list >>>>>>>> es-discuss at mozilla.org >>>>>>>> https://mail.mozilla.org/listinfo/es-discuss >>>>>>> >>>>>>> >>>>>>> >>>>>>> -- >>>>>>> Cheers, >>>>>>> --MarkM >>>>>>> >>>>>>> _______________________________________________ >>>>>>> es-discuss mailing list >>>>>>> es-discuss at mozilla.org >>>>>>> https://mail.mozilla.org/listinfo/es-discuss >>>>>> >>>>>> >>>>>> _______________________________________________ >>>>>> es-discuss mailing list >>>>>> es-discuss at mozilla.org >>>>>> https://mail.mozilla.org/listinfo/es-discuss >>>>> >>>>> >>>>> _______________________________________________ >>>>> es-discuss mailing list >>>>> es-discuss at mozilla.org >>>>> https://mail.mozilla.org/listinfo/es-discuss > -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20150909/675d2e33/attachment.html>
For posterity: http://wiki.ecmascript.org/doku.php?id=proposals:hashcodes http://wiki.ecmascript.org/doku.php?id=discussion:hashcodes (from ES4 daze, a long time ago.) /be Michael McGlothlin 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). > > > Thanks, > Michael McGlothlin > Sent from my iPhone > _______________________________________________ > es-discuss mailing list > es-discuss at mozilla.org > https://mail.mozilla.org/listinfo/es-discuss >
I'm only seeing a couple comments there - was that all or am I missing something?
I think hashes as an overwritable method that returns a number is pretty standard and let's you combine member hashes with simple Bitwise ops. Could have on value types and a default on Object that would combine hash of own props that were value types or frozen.
Thanks, Michael McGlothlin Sent from my iPhone
I'm only seeing a couple comments there - was that all or am I missing something? I think hashes as an overwritable method that returns a number is pretty standard and let's you combine member hashes with simple Bitwise ops. Could have on value types and a default on Object that would combine hash of own props that were value types or frozen. Thanks, Michael McGlothlin Sent from my iPhone > On Sep 9, 2015, at 9:53 AM, Brendan Eich <brendan at mozilla.org> wrote: > > For posterity: > > http://wiki.ecmascript.org/doku.php?id=proposals:hashcodes > http://wiki.ecmascript.org/doku.php?id=discussion:hashcodes > > (from ES4 daze, a long time ago.) > > /be > > Michael McGlothlin 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). >> >> >> Thanks, >> Michael McGlothlin >> Sent from my iPhone >> _______________________________________________ >> es-discuss mailing list >> es-discuss at mozilla.org >> https://mail.mozilla.org/listinfo/es-discuss >>
Michael McGlothlin wrote:
I'm only seeing a couple comments there - was that all or am I missing something?
Did you read the note about security?
I think hashes as an overwritable method that returns a number is pretty standard and let's you combine member hashes with simple Bitwise ops. Could have on value types and a default on Object that would combine hash of own props that were value types or frozen.
As others noted, WeakMap sucks a lot of the O2 away from any hashcode proposal.
Map and Set do need a way for objects (not value types) to hash and compare, IMHO, but that was deferred to ES7/2016 or later.
Michael McGlothlin wrote: > I'm only seeing a couple comments there - was that all or am I missing something? Did you read the note about security? > I think hashes as an overwritable method that returns a number is pretty standard and let's you combine member hashes with simple Bitwise ops. Could have on value types and a default on Object that would combine hash of own props that were value types or frozen. As others noted, WeakMap sucks a lot of the O2 away from any hashcode proposal. Map and Set do need a way for objects (not value types) to hash and compare, IMHO, but that was deferred to ES7/2016 or later. /be
One issue I did notice with WeakMaps is that because it can't use primative values as keys that they'll throw errors unless otherwise handled. Checking if they are an instanceof Object and if not using a Map instead seems to work.
I wouldn't think using primatives as WeakMap keys would be an issue.
const identities = new WeakMap(); const primatives = new Map(); let OBJECT_ID = 0;
function getObjectIdentity() { if ( this instanceof Object ) { if ( !identities.has( this ) ) { identities.set( this, ++OBJECT_ID ); } return identities.get( this ); }
if ( !primatives.has( this ) ) {
primatives.set( this, ++OBJECT_ID );
}
return primatives.get( this );
}
One issue I did notice with WeakMaps is that because it can't use primative values as keys that they'll throw errors unless otherwise handled. Checking if they are an instanceof Object and if not using a Map instead seems to work. I wouldn't think using primatives as WeakMap keys would be an issue. const identities = new WeakMap(); const primatives = new Map(); let OBJECT_ID = 0; function getObjectIdentity() { if ( this instanceof Object ) { if ( !identities.has( this ) ) { identities.set( this, ++OBJECT_ID ); } return identities.get( this ); } if ( !primatives.has( this ) ) { primatives.set( this, ++OBJECT_ID ); } return primatives.get( this ); } On Wed, Sep 9, 2015 at 8:14 AM, Michael McGlothlin < mike.mcglothlin at gmail.com> wrote: > I think using WeakMaps is a good idea. Using a Symbol will keep from > cluttering the namespace with names others might want to use (id,label,etc) > and seems more consistent than some external means of accessing the id. > When I tried defining the value before I ran into issues with the prototype > chain which is why I was using get and a second 'hidden' property. Using > WeakMap seems like it'd fix the issue. > > Can't say I've used Object.assign much - will have to experiment with it. > > Thanks, > Michael McGlothlin > Sent from my iPhone > > On Sep 9, 2015, at 7:34 AM, Andrea Giammarchi <andrea.giammarchi at gmail.com> > wrote: > > Well, to start with, you don't hide symbols, these are all exposed through > `Object.getOwnPropertySymbols` or Reflect.ownKeys so for your use case > looks like using just a non enumerable property would do as well. > And btw, even if Symbols don't show up in for/in and Object.keys, these > are by default also enumerable so Object.assign will copy them around > (reason I've used defineProperty with the value instead of just setting the > symbol, so it's non enumerable by default). > > As summary: do you want to be sure that object has a unique "lable" or > "id" associated with it? Use WeakMaps, otherwise be prepared to possible > clones around if some library uses Object.assign thinking that's a good way > to copy properties around. > > I still suggest the Labeler approach that Mark previously indicated. > > Regards > > > > > On Wed, Sep 9, 2015 at 1:15 PM, Michael McGlothlin < > mike.mcglothlin at gmail.com> wrote: > >> Using frozen objects and such seems a lot more complex to me than just >> using a property. Using a symbol to hide the property from cluttering seems >> to go along with how other things are being done. And it's my understanding >> that other than hiding it that using a Symbol for a name is nothing special >> - does it have some negative factor that should make using it less >> attractive? >> >> 📱 Michael McGlothlin >> >> On Sep 9, 2015, at 5:36 AM, Andrea Giammarchi < >> andrea.giammarchi at gmail.com> wrote: >> >> 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 <michalwadas at 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.giammarchi at 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.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; >>>>> } >>>>> return this[ identity ]; >>>>> } >>>>> } ); >>>>> >>>>> On Tue, Sep 8, 2015 at 1:44 PM, Mark S. Miller <erights at 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 <joeedh at 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" <joeedh at gmail.com> >>>>>>> Date: Sep 8, 2015 11:15 AM >>>>>>> Subject: Re: Object id, hash, etc? >>>>>>> To: "Garrett Smith" <dhtmlkitchen at 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" <dhtmlkitchen at gmail.com> >>>>>>> wrote: >>>>>>> >>>>>>>> On 9/8/15, Michael McGlothlin <mike.mcglothlin at 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 at mozilla.org >>>>>>>> https://mail.mozilla.org/listinfo/es-discuss >>>>>>>> >>>>>>> >>>>>>> _______________________________________________ >>>>>>> es-discuss mailing list >>>>>>> es-discuss at mozilla.org >>>>>>> https://mail.mozilla.org/listinfo/es-discuss >>>>>>> >>>>>>> >>>>>> >>>>>> >>>>>> -- >>>>>> Cheers, >>>>>> --MarkM >>>>>> >>>>>> _______________________________________________ >>>>>> es-discuss mailing list >>>>>> es-discuss at mozilla.org >>>>>> https://mail.mozilla.org/listinfo/es-discuss >>>>>> >>>>>> >>>>> >>>>> _______________________________________________ >>>>> es-discuss mailing list >>>>> es-discuss at mozilla.org >>>>> https://mail.mozilla.org/listinfo/es-discuss >>>>> >>>>> >>>> >>>> _______________________________________________ >>>> es-discuss mailing list >>>> es-discuss at mozilla.org >>>> https://mail.mozilla.org/listinfo/es-discuss >>>> >>>> >>> >> > -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20150910/f45d4000/attachment.html>
I would avoid using Object identities on primitives as you can do ===
comparisons if IDs are serialized and have differing IDs for equivalent
objects.
I would avoid using Object identities on primitives as you can do `===` comparisons if IDs are serialized and have differing IDs for equivalent objects. On Thu, Sep 10, 2015 at 1:02 PM, Michael McGlothlin < mike.mcglothlin at gmail.com> wrote: > One issue I did notice with WeakMaps is that because it can't use > primative values as keys that they'll throw errors unless otherwise > handled. Checking if they are an instanceof Object and if not using a Map > instead seems to work. > > I wouldn't think using primatives as WeakMap keys would be an issue. > > > const identities = new WeakMap(); > const primatives = new Map(); > let OBJECT_ID = 0; > > function getObjectIdentity() { > if ( this instanceof Object ) { > if ( !identities.has( this ) ) { > identities.set( this, ++OBJECT_ID ); > } > return identities.get( this ); > } > > if ( !primatives.has( this ) ) { > primatives.set( this, ++OBJECT_ID ); > } > return primatives.get( this ); > } > > > On Wed, Sep 9, 2015 at 8:14 AM, Michael McGlothlin < > mike.mcglothlin at gmail.com> wrote: > >> I think using WeakMaps is a good idea. Using a Symbol will keep from >> cluttering the namespace with names others might want to use (id,label,etc) >> and seems more consistent than some external means of accessing the id. >> When I tried defining the value before I ran into issues with the prototype >> chain which is why I was using get and a second 'hidden' property. Using >> WeakMap seems like it'd fix the issue. >> >> Can't say I've used Object.assign much - will have to experiment with it. >> >> Thanks, >> Michael McGlothlin >> Sent from my iPhone >> >> On Sep 9, 2015, at 7:34 AM, Andrea Giammarchi < >> andrea.giammarchi at gmail.com> wrote: >> >> Well, to start with, you don't hide symbols, these are all exposed >> through `Object.getOwnPropertySymbols` or Reflect.ownKeys so for your use >> case looks like using just a non enumerable property would do as well. >> And btw, even if Symbols don't show up in for/in and Object.keys, these >> are by default also enumerable so Object.assign will copy them around >> (reason I've used defineProperty with the value instead of just setting the >> symbol, so it's non enumerable by default). >> >> As summary: do you want to be sure that object has a unique "lable" or >> "id" associated with it? Use WeakMaps, otherwise be prepared to possible >> clones around if some library uses Object.assign thinking that's a good way >> to copy properties around. >> >> I still suggest the Labeler approach that Mark previously indicated. >> >> Regards >> >> >> >> >> On Wed, Sep 9, 2015 at 1:15 PM, Michael McGlothlin < >> mike.mcglothlin at gmail.com> wrote: >> >>> Using frozen objects and such seems a lot more complex to me than just >>> using a property. Using a symbol to hide the property from cluttering seems >>> to go along with how other things are being done. And it's my understanding >>> that other than hiding it that using a Symbol for a name is nothing special >>> - does it have some negative factor that should make using it less >>> attractive? >>> >>> 📱 Michael McGlothlin >>> >>> On Sep 9, 2015, at 5:36 AM, Andrea Giammarchi < >>> andrea.giammarchi at gmail.com> wrote: >>> >>> 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 <michalwadas at 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.giammarchi at 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.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; >>>>>> } >>>>>> return this[ identity ]; >>>>>> } >>>>>> } ); >>>>>> >>>>>> On Tue, Sep 8, 2015 at 1:44 PM, Mark S. Miller <erights at 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 <joeedh at 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" <joeedh at gmail.com> >>>>>>>> Date: Sep 8, 2015 11:15 AM >>>>>>>> Subject: Re: Object id, hash, etc? >>>>>>>> To: "Garrett Smith" <dhtmlkitchen at 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" <dhtmlkitchen at gmail.com> >>>>>>>> wrote: >>>>>>>> >>>>>>>>> On 9/8/15, Michael McGlothlin <mike.mcglothlin at 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 at mozilla.org >>>>>>>>> https://mail.mozilla.org/listinfo/es-discuss >>>>>>>>> >>>>>>>> >>>>>>>> _______________________________________________ >>>>>>>> es-discuss mailing list >>>>>>>> es-discuss at mozilla.org >>>>>>>> https://mail.mozilla.org/listinfo/es-discuss >>>>>>>> >>>>>>>> >>>>>>> >>>>>>> >>>>>>> -- >>>>>>> Cheers, >>>>>>> --MarkM >>>>>>> >>>>>>> _______________________________________________ >>>>>>> es-discuss mailing list >>>>>>> es-discuss at mozilla.org >>>>>>> https://mail.mozilla.org/listinfo/es-discuss >>>>>>> >>>>>>> >>>>>> >>>>>> _______________________________________________ >>>>>> es-discuss mailing list >>>>>> es-discuss at mozilla.org >>>>>> https://mail.mozilla.org/listinfo/es-discuss >>>>>> >>>>>> >>>>> >>>>> _______________________________________________ >>>>> es-discuss mailing list >>>>> es-discuss at mozilla.org >>>>> https://mail.mozilla.org/listinfo/es-discuss >>>>> >>>>> >>>> >>> >> > > _______________________________________________ > es-discuss mailing list > es-discuss at mozilla.org > https://mail.mozilla.org/listinfo/es-discuss > > -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20150910/89645e8e/attachment-0001.html>
On Thu, Sep 10, 2015 at 11:02 AM, Michael McGlothlin <mike.mcglothlin at gmail.com> wrote:
One issue I did notice with WeakMaps is that because it can't use primative values as keys that they'll throw errors unless otherwise handled. Checking if they are an instanceof Object and if not using a Map instead seems to work.
I wouldn't think using primatives as WeakMap keys would be an issue.
Primitives have no notion of "reference" or "liveness". They can and are destroyed and recreated all the time, or shared across multiple independent non-communicating contexts. As such, they aren't a reliable key for WeakMaps, which keep their value alive "as long as the key is alive". Using a primitive string as a key can inadvertently leak the value for a long time, as the key might be interned and long-lived, far past the point when your code has dropped the intended references to it.
On Thu, Sep 10, 2015 at 11:02 AM, Michael McGlothlin <mike.mcglothlin at gmail.com> wrote: > One issue I did notice with WeakMaps is that because it can't use primative values as keys that they'll throw errors unless otherwise handled. Checking if they are an instanceof Object and if not using a Map instead seems to work. > > I wouldn't think using primatives as WeakMap keys would be an issue. Primitives have no notion of "reference" or "liveness". They can and are destroyed and recreated all the time, or shared across multiple independent non-communicating contexts. As such, they aren't a reliable key for WeakMaps, which keep their value alive "as long as the key is alive". Using a primitive string as a key can inadvertently leak the value for a long time, as the key might be interned and long-lived, far past the point when your code has dropped the intended references to it. ~TJ
in that way you are avoiding benefits provided by WeakMap + objects from other realms are not instanceof Object + Object.create(null) instanceof Object is false.
You probably want to use the WeakMap with anything that is typeof "object" or typeof "function" and for everything else you can use the value itself as unique identity for itself.
const identities = new WeakMap();
let OBJECT_ID = 0;
function getObjectIdentity(obj) {
switch (typeof obj) {
case 'object':
case 'function':
let id = identities.get(obj);
if (!id) {
identities.set(obj, (id = ++OBJECT_ID));
}
return id;
default:
return obj;
}
}
Watch out collisions with possible integers or throw whenever the typeof is
number. You could use this
only in "use strict" capable environment
otherwise primitives are going to be converted at runtime as objects with
every single time a different identity.
in that way you are avoiding benefits provided by WeakMap + objects from other realms are not instanceof Object + Object.create(null) instanceof Object is false. You probably want to use the WeakMap with anything that is typeof "object" or typeof "function" and for everything else you can use the value itself as unique identity for itself. ```js const identities = new WeakMap(); let OBJECT_ID = 0; function getObjectIdentity(obj) { switch (typeof obj) { case 'object': case 'function': let id = identities.get(obj); if (!id) { identities.set(obj, (id = ++OBJECT_ID)); } return id; default: return obj; } } ``` Watch out collisions with possible integers or throw whenever the typeof is number. You could use `this` only in "use strict" capable environment otherwise primitives are going to be converted at runtime as objects with every single time a different identity. Regards On Thu, Sep 10, 2015 at 8:02 PM, Michael McGlothlin < mike.mcglothlin at gmail.com> wrote: > One issue I did notice with WeakMaps is that because it can't use > primative values as keys that they'll throw errors unless otherwise > handled. Checking if they are an instanceof Object and if not using a Map > instead seems to work. > > I wouldn't think using primatives as WeakMap keys would be an issue. > > > const identities = new WeakMap(); > const primatives = new Map(); > let OBJECT_ID = 0; > > function getObjectIdentity() { > if ( this instanceof Object ) { > if ( !identities.has( this ) ) { > identities.set( this, ++OBJECT_ID ); > } > return identities.get( this ); > } > > if ( !primatives.has( this ) ) { > primatives.set( this, ++OBJECT_ID ); > } > return primatives.get( this ); > } > > > On Wed, Sep 9, 2015 at 8:14 AM, Michael McGlothlin < > mike.mcglothlin at gmail.com> wrote: > >> I think using WeakMaps is a good idea. Using a Symbol will keep from >> cluttering the namespace with names others might want to use (id,label,etc) >> and seems more consistent than some external means of accessing the id. >> When I tried defining the value before I ran into issues with the prototype >> chain which is why I was using get and a second 'hidden' property. Using >> WeakMap seems like it'd fix the issue. >> >> Can't say I've used Object.assign much - will have to experiment with it. >> >> Thanks, >> Michael McGlothlin >> Sent from my iPhone >> >> On Sep 9, 2015, at 7:34 AM, Andrea Giammarchi < >> andrea.giammarchi at gmail.com> wrote: >> >> Well, to start with, you don't hide symbols, these are all exposed >> through `Object.getOwnPropertySymbols` or Reflect.ownKeys so for your use >> case looks like using just a non enumerable property would do as well. >> And btw, even if Symbols don't show up in for/in and Object.keys, these >> are by default also enumerable so Object.assign will copy them around >> (reason I've used defineProperty with the value instead of just setting the >> symbol, so it's non enumerable by default). >> >> As summary: do you want to be sure that object has a unique "lable" or >> "id" associated with it? Use WeakMaps, otherwise be prepared to possible >> clones around if some library uses Object.assign thinking that's a good way >> to copy properties around. >> >> I still suggest the Labeler approach that Mark previously indicated. >> >> Regards >> >> >> >> >> On Wed, Sep 9, 2015 at 1:15 PM, Michael McGlothlin < >> mike.mcglothlin at gmail.com> wrote: >> >>> Using frozen objects and such seems a lot more complex to me than just >>> using a property. Using a symbol to hide the property from cluttering seems >>> to go along with how other things are being done. And it's my understanding >>> that other than hiding it that using a Symbol for a name is nothing special >>> - does it have some negative factor that should make using it less >>> attractive? >>> >>> 📱 Michael McGlothlin >>> >>> On Sep 9, 2015, at 5:36 AM, Andrea Giammarchi < >>> andrea.giammarchi at gmail.com> wrote: >>> >>> 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 <michalwadas at 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.giammarchi at 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.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; >>>>>> } >>>>>> return this[ identity ]; >>>>>> } >>>>>> } ); >>>>>> >>>>>> On Tue, Sep 8, 2015 at 1:44 PM, Mark S. Miller <erights at 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 <joeedh at 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" <joeedh at gmail.com> >>>>>>>> Date: Sep 8, 2015 11:15 AM >>>>>>>> Subject: Re: Object id, hash, etc? >>>>>>>> To: "Garrett Smith" <dhtmlkitchen at 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" <dhtmlkitchen at gmail.com> >>>>>>>> wrote: >>>>>>>> >>>>>>>>> On 9/8/15, Michael McGlothlin <mike.mcglothlin at 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 at mozilla.org >>>>>>>>> https://mail.mozilla.org/listinfo/es-discuss >>>>>>>>> >>>>>>>> >>>>>>>> _______________________________________________ >>>>>>>> es-discuss mailing list >>>>>>>> es-discuss at mozilla.org >>>>>>>> https://mail.mozilla.org/listinfo/es-discuss >>>>>>>> >>>>>>>> >>>>>>> >>>>>>> >>>>>>> -- >>>>>>> Cheers, >>>>>>> --MarkM >>>>>>> >>>>>>> _______________________________________________ >>>>>>> es-discuss mailing list >>>>>>> es-discuss at mozilla.org >>>>>>> https://mail.mozilla.org/listinfo/es-discuss >>>>>>> >>>>>>> >>>>>> >>>>>> _______________________________________________ >>>>>> es-discuss mailing list >>>>>> es-discuss at mozilla.org >>>>>> https://mail.mozilla.org/listinfo/es-discuss >>>>>> >>>>>> >>>>> >>>>> _______________________________________________ >>>>> es-discuss mailing list >>>>> es-discuss at mozilla.org >>>>> https://mail.mozilla.org/listinfo/es-discuss >>>>> >>>>> >>>> >>> >> > -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20150911/da440883/attachment-0001.html>
Tab Atkins Jr. wrote:
Using a primitive string as a key can inadvertently leak the value for a long time, as the key might be interned and long-lived, far past the point when your code has dropped the intended references to it.
Indeed. Strings do not have observable reference semantics at all, so any WeakMap accepting a "foo" key would have to keep the entry alive forever, as "foo" could be uttered by later code. Same for 42 ;-). You might think primitives could be live or dead but only objects are like that.
Tab Atkins Jr. wrote: > Using a primitive string as a key can > inadvertently leak the value for a long time, as the key might be > interned and long-lived, far past the point when your code has dropped > the intended references to it. Indeed. Strings do not have observable reference semantics at all, so any WeakMap accepting a "foo" key would have to keep the entry alive forever, as "foo" could be uttered by later code. Same for 42 ;-). You might think primitives could be live or dead but only objects are like that. /be
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).
Thanks, Michael McGlothlin Sent from my iPhone