Re: Object id, hash, etc?

2015-09-11 Thread Brendan Eich

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
___
es-discuss mailing list
es-discuss@mozilla.org
https://mail.mozilla.org/listinfo/es-discuss


Re: Object id, hash, etc?

2015-09-11 Thread Andrea Giammarchi
ler seems again a more appropriate
>>> pattern for this problem.
>>>
>>> Just my thoughts
>>>
>>> On Wed, Sep 9, 2015 at 10:51 AM, Michał Wadas <michalwa...@gmail.com>
>>> wrote:
>>>
>>>> Following solution will work:
>>>>
>>>> (function(){
>>>> const wm = new WeakMap();
>>>> Symbol.identity = Symbol( 'Symbol.identity' );
>>>> let i = 0;
>>>> Object.defineProperty(
>>>>   Object.prototype,
>>>>   Symbol.identity,
>>>>   {
>>>> get: function () {
>>>>   if (wm.has(this)) return wm.get(this);
>>>>   wm.set(this, Symbol(i++));
>>>>   return wm.get(this);
>>>> }
>>>>   }
>>>> );
>>>> })()
>>>>
>>>>
>>>> 2015-09-08 23:29 GMT+02:00 Andrea Giammarchi <
>>>> andrea.giammar...@gmail.com>:
>>>>
>>>>> as side note, that's just a lazy assignment that doesn't need two
>>>>> symbols and a constant get invoke ...
>>>>>
>>>>> ```js
>>>>> Symbol.identity = Symbol( 'Symbol.identity' );
>>>>> var OBJECT_ID = 0;
>>>>> Object.defineProperty(
>>>>>   Object.prototype,
>>>>>   Symbol.identity,
>>>>>   {
>>>>> get: function () {
>>>>>   // first time this is invoked ... and no more ...
>>>>>   Object.defineProperty(this, Symbol.identity, {value:
>>>>> ++OBJECT_ID});
>>>>>   // from now own, direct property access \m/
>>>>>   return this[Symbol.identity];
>>>>> }
>>>>>   }
>>>>> );
>>>>> ```
>>>>>
>>>>> Regards
>>>>>
>>>>> On Tue, Sep 8, 2015 at 9:57 PM, Michael McGlothlin <
>>>>> mike.mcgloth...@gmail.com> wrote:
>>>>>
>>>>>> I try to keep it pretty simple. It's not fancy but the times you want
>>>>>> fast and dirty information like this are the same times you don't want to
>>>>>> have to define it manually.
>>>>>>
>>>>>>  Symbol.identity = Symbol( 'Symbol.identity' );
>>>>>>  const identity = Symbol( 'identity' );
>>>>>>  var OBJECT_ID = 0;
>>>>>>  Object.defineProperty( Object.prototype, Symbol.identity, {
>>>>>>   get: () => {
>>>>>>if ( !Object.hasOwnProperty.call( this, identity ) ) {
>>>>>> this[ identity ] = ++OBJECT_ID;
>>>>>>}
>>>>>>return this[ identity ];
>>>>>>   }
>>>>>>  } );
>>>>>>
>>>>>> On Tue, Sep 8, 2015 at 1:44 PM, Mark S. Miller <erig...@google.com>
>>>>>> wrote:
>>>>>>
>>>>>>> See Labeler at
>>>>>>>
>>>>>>> http://wiki.ecmascript.org/doku.php?id=harmony:weak_maps#unique_labeler
>>>>>>>
>>>>>>>
>>>>>>>
>>>>>>> On Tue, Sep 8, 2015 at 11:16 AM, joe <joe...@gmail.com> wrote:
>>>>>>>
>>>>>>>> Didn't send to list, something is wrong with my reply all. Sorry
>>>>>>>> about that. Stupid mobile gmail.
>>>>>>>> -- Forwarded message --
>>>>>>>> From: "joe" <joe...@gmail.com>
>>>>>>>> Date: Sep 8, 2015 11:15 AM
>>>>>>>> Subject: Re: Object id, hash, etc?
>>>>>>>> To: "Garrett Smith" <dhtmlkitc...@gmail.com>
>>>>>>>> Cc:
>>>>>>>>
>>>>>>>> I agree with this request. This is the logical complement to
>>>>>>>> valueof, I think. And yes, for most ID use cases this isn't a good 
>>>>>>>> fit, but
>>>>>>>> we're not talking about the general case, just the cases where a python
>>>>>>>> style id() function *is* appropriate.
>>>>>>>>
>>>>>>>> Joe
>>>>>>>> On Sep 8, 2015 9:08 AM, "Garrett Smith" <dhtmlkitc...@gmail.com>
>>>>>>>> wrote:
>>>>>>>>
>>>>>>>>> On 9/8/15, Michael McGlothlin <mike.mcgloth...@gmail.com> wrote:

Re: Object id, hash, etc?

2015-09-10 Thread Tab Atkins Jr.
On Thu, Sep 10, 2015 at 11:02 AM, Michael McGlothlin
 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
___
es-discuss mailing list
es-discuss@mozilla.org
https://mail.mozilla.org/listinfo/es-discuss


Re: Object id, hash, etc?

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


Re: Object id, hash, etc?

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


Re: Object id, hash, etc?

2015-09-09 Thread Andrea Giammarchi
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.mcgloth...@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.giammar...@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 <michalwa...@gmail.com>
> wrote:
>
>> Following solution will work:
>>
>> (function(){
>> const wm = new WeakMap();
>> Symbol.identity = Symbol( 'Symbol.identity' );
>> let i = 0;
>> Object.defineProperty(
>>   Object.prototype,
>>   Symbol.identity,
>>   {
>> get: function () {
>>   if (wm.has(this)) return wm.get(this);
>>   wm.set(this, Symbol(i++));
>>   return wm.get(this);
>> }
>>   }
>> );
>> })()
>>
>>
>> 2015-09-08 23:29 GMT+02:00 Andrea Giammarchi <andrea.giammar...@gmail.com
>> >:
>>
>>> as side note, that's just a lazy assignment that doesn't need two
>>> symbols and a constant get invoke ...
>>>
>>> ```js
>>> Symbol.identity = Symbol( 'Symbol.identity' );
>>> var OBJECT_ID = 0;
>>> Object.defineProperty(
>>>   Object.prototype,
>>>   Symbol.identity,
>>>   {
>>> get: function () {
>>>   // first time this is invoked ... and no more ...
>>>   Object.defineProperty(this, Symbol.identity, {value: ++OBJECT_ID});
>>>   // from now own, direct property access \m/
>>>   return this[Symbol.identity];
>>> }
>>>   }
>>> );
>>> ```
>>>
>>> Regards
>>>
>>> On Tue, Sep 8, 2015 at 9:57 PM, Michael McGlothlin <
>>> mike.mcgloth...@gmail.com> wrote:
>>>
>>>> I try to keep it pretty simple. It's not fancy but the times you want
>>>> fast and dirty information like this are the same times you don't want to
>>>> have to define it manually.
>>>>
>>>>  Symbol.identity = Symbol( 'Symbol.identity' );
>>>>  const identity = Symbol( 'identity' );
>>>>  var OBJECT_ID = 0;
>>>>  Object.defineProperty( Object.prototype, Symbol.identity, {
>>>>   get: () => {
>>>>if ( !Object.hasOwnProperty.call( this, identity ) ) {
>>>> this[ identity ] = ++OBJECT_ID;
>>>>}
>>>>return this[ identity ];
>>>>   }
>>>>  } );
>>>>
>>>> On Tue, Sep 8, 2015 at 1:44 PM, Mark S. Miller <erig...@google.com>
>>>> wrote:
>>>>
>>>>> See Labeler at
>>>>> http://wiki.ecmascript.org/doku.php?id=harmony:weak_maps#unique_labeler
>>>>>
>>>>>
>>>>>
>>>>> On Tue, Sep 8, 2015 at 11:16 AM, joe <joe...@gmail.com> wrote:
>>>>>
>>>>>> Didn't send to list, something is wrong with my reply all. Sorry
>>>>>> about that. Stupid mobile gmail.
>>>>>> -- Forwarded message --
>

Re: Object id, hash, etc?

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

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

Just my thoughts

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

> Following solution will work:
>
> (function(){
> const wm = new WeakMap();
> Symbol.identity = Symbol( 'Symbol.identity' );
> let i = 0;
> Object.defineProperty(
>   Object.prototype,
>   Symbol.identity,
>   {
> get: function () {
>   if (wm.has(this)) return wm.get(this);
>   wm.set(this, Symbol(i++));
>   return wm.get(this);
> }
>   }
> );
> })()
>
>
> 2015-09-08 23:29 GMT+02:00 Andrea Giammarchi <andrea.giammar...@gmail.com>
> :
>
>> as side note, that's just a lazy assignment that doesn't need two symbols
>> and a constant get invoke ...
>>
>> ```js
>> Symbol.identity = Symbol( 'Symbol.identity' );
>> var OBJECT_ID = 0;
>> Object.defineProperty(
>>   Object.prototype,
>>   Symbol.identity,
>>   {
>> get: function () {
>>   // first time this is invoked ... and no more ...
>>   Object.defineProperty(this, Symbol.identity, {value: ++OBJECT_ID});
>>   // from now own, direct property access \m/
>>   return this[Symbol.identity];
>> }
>>   }
>> );
>> ```
>>
>> Regards
>>
>> On Tue, Sep 8, 2015 at 9:57 PM, Michael McGlothlin <
>> mike.mcgloth...@gmail.com> wrote:
>>
>>> I try to keep it pretty simple. It's not fancy but the times you want
>>> fast and dirty information like this are the same times you don't want to
>>> have to define it manually.
>>>
>>>  Symbol.identity = Symbol( 'Symbol.identity' );
>>>  const identity = Symbol( 'identity' );
>>>  var OBJECT_ID = 0;
>>>  Object.defineProperty( Object.prototype, Symbol.identity, {
>>>   get: () => {
>>>if ( !Object.hasOwnProperty.call( this, identity ) ) {
>>> this[ identity ] = ++OBJECT_ID;
>>>}
>>>return this[ identity ];
>>>   }
>>>  } );
>>>
>>> On Tue, Sep 8, 2015 at 1:44 PM, Mark S. Miller <erig...@google.com>
>>> wrote:
>>>
>>>> See Labeler at
>>>> http://wiki.ecmascript.org/doku.php?id=harmony:weak_maps#unique_labeler
>>>>
>>>>
>>>>
>>>> On Tue, Sep 8, 2015 at 11:16 AM, joe <joe...@gmail.com> wrote:
>>>>
>>>>> Didn't send to list, something is wrong with my reply all. Sorry about
>>>>> that. Stupid mobile gmail.
>>>>> -- Forwarded message --
>>>>> From: "joe" <joe...@gmail.com>
>>>>> Date: Sep 8, 2015 11:15 AM
>>>>> Subject: Re: Object id, hash, etc?
>>>>> To: "Garrett Smith" <dhtmlkitc...@gmail.com>
>>>>> Cc:
>>>>>
>>>>> I agree with this request. This is the logical complement to valueof,
>>>>> I think. And yes, for most ID use cases this isn't a good fit, but we're
>>>>> not talking about the general case, just the cases where a python style
>>>>> id() function *is* appropriate.
>>>>>
>>>>> Joe
>>>>> On Sep 8, 2015 9:08 AM, "Garrett Smith" <dhtmlkitc...@gmail.com>
>>>>> wrote:
>>>>>
>>>>>> On 9/8/15, Michael McGlothlin <mike.mcgloth...@gmail.com> wrote:
>>>>>> > Is there a reason not to provide an object id and hash value as
>>>>>> other
>>>>>> > languages often do? I find myself defining an identity symbol on
>>>>>> objects
>>>>>> > with a value from a simple global counter. It makes it easier to
>>>>>> debug if I
>>>>>> > can just look at a log and see what objects and functions were
>>>>>> active.
>>>>>> >
>>>>>> > Likewise a hash value would simplify a lot of comparisons and make
>>>>>> it easier
>>>>>> > to debug.
>>>>>> >
>>>>>> > I especially find the ID useful when working with bound functions
>>>>>> as they
>>>>>> > can 

Re: Object id, hash, etc?

2015-09-09 Thread Michał Wadas
Following solution will work:

(function(){
const wm = new WeakMap();
Symbol.identity = Symbol( 'Symbol.identity' );
let i = 0;
Object.defineProperty(
  Object.prototype,
  Symbol.identity,
  {
get: function () {
  if (wm.has(this)) return wm.get(this);
  wm.set(this, Symbol(i++));
  return wm.get(this);
}
  }
);
})()


2015-09-08 23:29 GMT+02:00 Andrea Giammarchi <andrea.giammar...@gmail.com>:

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

Re: Object id, hash, etc?

2015-09-09 Thread 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.giammar...@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 <michalwa...@gmail.com> wrote:
>> Following solution will work:
>> 
>> (function(){
>> const wm = new WeakMap();
>> Symbol.identity = Symbol( 'Symbol.identity' );
>> let i = 0;
>> Object.defineProperty(
>>   Object.prototype,
>>   Symbol.identity,
>>   {
>> get: function () {
>>   if (wm.has(this)) return wm.get(this);
>>   wm.set(this, Symbol(i++));
>>   return wm.get(this);
>> }
>>   }
>> );
>> })()
>> 
>> 
>> 2015-09-08 23:29 GMT+02:00 Andrea Giammarchi <andrea.giammar...@gmail.com>:
>>> as side note, that's just a lazy assignment that doesn't need two symbols 
>>> and a constant get invoke ...
>>> 
>>> ```js
>>> Symbol.identity = Symbol( 'Symbol.identity' );
>>> var OBJECT_ID = 0;
>>> Object.defineProperty(
>>>   Object.prototype,
>>>   Symbol.identity,
>>>   {
>>> get: function () {
>>>   // first time this is invoked ... and no more ...
>>>   Object.defineProperty(this, Symbol.identity, {value: ++OBJECT_ID});
>>>   // from now own, direct property access \m/
>>>   return this[Symbol.identity];
>>> }
>>>   }
>>> );
>>> ```
>>> 
>>> Regards 
>>> 
>>>> On Tue, Sep 8, 2015 at 9:57 PM, Michael McGlothlin 
>>>> <mike.mcgloth...@gmail.com> wrote:
>>>> I try to keep it pretty simple. It's not fancy but the times you want fast 
>>>> and dirty information like this are the same times you don't want to have 
>>>> to define it manually.
>>>> 
>>>>  Symbol.identity = Symbol( 'Symbol.identity' );
>>>>  const identity = Symbol( 'identity' );
>>>>  var OBJECT_ID = 0;
>>>>  Object.defineProperty( Object.prototype, Symbol.identity, {
>>>>   get: () => {
>>>>if ( !Object.hasOwnProperty.call( this, identity ) ) {
>>>> this[ identity ] = ++OBJECT_ID;
>>>>}
>>>>return this[ identity ];
>>>>   }
>>>>  } );
>>>> 
>>>>> On Tue, Sep 8, 2015 at 1:44 PM, Mark S. Miller <erig...@google.com> wrote:
>>>>> See Labeler at
>>>>> http://wiki.ecmascript.org/doku.php?id=harmony:weak_maps#unique_labeler
>>>>> 
>>>>> 
>>>>> 
>>>>>> On Tue, Sep 8, 2015 at 11:16 AM, joe <joe...@gmail.com> wrote:
>>>>>> Didn't send to list, something is wrong with my reply all. Sorry about 
>>>>>> that. Stupid mobile gmail.
>>>>>> 
>>>>>> -- Forwarded message --
>>>>>> From: "joe" <joe...@gmail.com>
>>>>>> Date: Sep 8, 2015 11:15 AM
>>>>>> Subject: Re: Object id, hash, etc?
>>>>>> To: "Garrett Smith" <dhtmlkitc...@gmail.com>
>>>>>> Cc: 
>>>>>> 
>>>>>> I agree with this request. This is the logical complement to valueof, I 
>>>>>> think. And yes, for most ID use cases this isn't a good fit, but we're 
>>>>>> not talking about the general case, just the cases where a python style 
>>>>>> id() function *is* appropriate.
>>>>>> 
>>>>>> Joe
>>>>>> 
>>>>>>> On Sep 8, 2015 9:08 AM, "Garrett Smith" <dhtmlkitc...@gmail.com> wrote:
>>>>>>> On 9/8/15, Michael McGlothlin <mike.mcgloth...@gmail.com> wrote:
>>>&

Re: Object id, hash, etc?

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


Re: Object id, hash, etc?

2015-09-09 Thread Brendan Eich

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@mozilla.org
https://mail.mozilla.org/listinfo/es-discuss


___
es-discuss mailing list
es-discuss@mozilla.org
https://mail.mozilla.org/listinfo/es-discuss


Re: Object id, hash, etc?

2015-09-09 Thread Brendan Eich

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
___
es-discuss mailing list
es-discuss@mozilla.org
https://mail.mozilla.org/listinfo/es-discuss


Re: Object id, hash, etc?

2015-09-09 Thread Michael McGlothlin
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  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@mozilla.org
>> https://mail.mozilla.org/listinfo/es-discuss
>> 
___
es-discuss mailing list
es-discuss@mozilla.org
https://mail.mozilla.org/listinfo/es-discuss


Re: Object id, hash, etc?

2015-09-08 Thread Mark S. Miller
See Labeler at
http://wiki.ecmascript.org/doku.php?id=harmony:weak_maps#unique_labeler



On Tue, Sep 8, 2015 at 11:16 AM, joe <joe...@gmail.com> wrote:

> Didn't send to list, something is wrong with my reply all. Sorry about
> that. Stupid mobile gmail.
> -- Forwarded message --
> From: "joe" <joe...@gmail.com>
> Date: Sep 8, 2015 11:15 AM
> Subject: Re: Object id, hash, etc?
> To: "Garrett Smith" <dhtmlkitc...@gmail.com>
> Cc:
>
> I agree with this request. This is the logical complement to valueof, I
> think. And yes, for most ID use cases this isn't a good fit, but we're not
> talking about the general case, just the cases where a python style id()
> function *is* appropriate.
>
> Joe
> On Sep 8, 2015 9:08 AM, "Garrett Smith" <dhtmlkitc...@gmail.com> wrote:
>
>> On 9/8/15, Michael McGlothlin <mike.mcgloth...@gmail.com> wrote:
>> > Is there a reason not to provide an object id and hash value as other
>> > languages often do? I find myself defining an identity symbol on objects
>> > with a value from a simple global counter. It makes it easier to debug
>> if I
>> > can just look at a log and see what objects and functions were active.
>> >
>> > Likewise a hash value would simplify a lot of comparisons and make it
>> easier
>> > to debug.
>> >
>> > I especially find the ID useful when working with bound functions as
>> they
>> > can be difficult to tell apart. I like to change toString() to provide
>> the
>> > ID followed by the code (flattened to one line).
>> >
>> >
>>
>> NFE's are safe to use where IE8 support isn't needed*.
>> (function aa(){}).name; // "aa"
>>
>> As for using Object IDs and Object Pooling, lexically-scoped values
>> have benefits over global ID generators (as your counter). The values
>> can be passed in as a parameter to the Factory (useful with Private
>> Proxy). There other benefits to this pattern regarding memory
>> management and application design. Contact me if you'd like to learn
>> more.
>>
>> * https://kangax.github.io/nfe/
>> --
>> Garrett
>> @xkit
>> ChordCycles.wordpress.com
>> garretts.github.io
>> personx.tumblr.com
>> ___
>> es-discuss mailing list
>> es-discuss@mozilla.org
>> https://mail.mozilla.org/listinfo/es-discuss
>>
>
> ___
> es-discuss mailing list
> es-discuss@mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss
>
>


-- 
Cheers,
--MarkM
___
es-discuss mailing list
es-discuss@mozilla.org
https://mail.mozilla.org/listinfo/es-discuss


Re: Object id, hash, etc?

2015-09-08 Thread Mark S. Miller
On Tue, Sep 8, 2015 at 1:57 PM, Michael McGlothlin <
mike.mcgloth...@gmail.com> wrote:

> I try to keep it pretty simple. It's not fancy but the times you want fast
> and dirty information like this are the same times you don't want to have
> to define it manually.
>
>  Symbol.identity = Symbol( 'Symbol.identity' );
>  const identity = Symbol( 'identity' );
>  var OBJECT_ID = 0;
>  Object.defineProperty( Object.prototype, Symbol.identity, {
>   get: () => {
>if ( !Object.hasOwnProperty.call( this, identity ) ) {
> this[ identity ] = ++OBJECT_ID;
>

Does not work on frozen objects.



>}
>return this[ identity ];
>   }
>  } );
>
> On Tue, Sep 8, 2015 at 1:44 PM, Mark S. Miller  wrote:
>
>> See Labeler at
>> http://wiki.ecmascript.org/doku.php?id=harmony:weak_maps#unique_labeler
>>
>>
>>
-- 
Cheers,
--MarkM
___
es-discuss mailing list
es-discuss@mozilla.org
https://mail.mozilla.org/listinfo/es-discuss


Re: Object id, hash, etc?

2015-09-08 Thread Tab Atkins Jr.
On Tue, Sep 8, 2015 at 2:07 PM, Mark S. Miller  wrote:
> On Tue, Sep 8, 2015 at 1:57 PM, Michael McGlothlin
>  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
___
es-discuss mailing list
es-discuss@mozilla.org
https://mail.mozilla.org/listinfo/es-discuss


Re: Object id, hash, etc?

2015-09-08 Thread Mark S. Miller
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.  wrote:

> On Tue, Sep 8, 2015 at 2:07 PM, Mark S. Miller  wrote:
> > On Tue, Sep 8, 2015 at 1:57 PM, Michael McGlothlin
> >  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
___
es-discuss mailing list
es-discuss@mozilla.org
https://mail.mozilla.org/listinfo/es-discuss


Re: Object id, hash, etc?

2015-09-08 Thread Andrea Giammarchi
as side note, that's just a lazy assignment that doesn't need two symbols
and a constant get invoke ...

```js
Symbol.identity = Symbol( 'Symbol.identity' );
var OBJECT_ID = 0;
Object.defineProperty(
  Object.prototype,
  Symbol.identity,
  {
get: function () {
  // first time this is invoked ... and no more ...
  Object.defineProperty(this, Symbol.identity, {value: ++OBJECT_ID});
  // from now own, direct property access \m/
  return this[Symbol.identity];
}
  }
);
```

Regards

On Tue, Sep 8, 2015 at 9:57 PM, Michael McGlothlin <
mike.mcgloth...@gmail.com> wrote:

> I try to keep it pretty simple. It's not fancy but the times you want fast
> and dirty information like this are the same times you don't want to have
> to define it manually.
>
>  Symbol.identity = Symbol( 'Symbol.identity' );
>  const identity = Symbol( 'identity' );
>  var OBJECT_ID = 0;
>  Object.defineProperty( Object.prototype, Symbol.identity, {
>   get: () => {
>if ( !Object.hasOwnProperty.call( this, identity ) ) {
> this[ identity ] = ++OBJECT_ID;
>}
>return this[ identity ];
>   }
>  } );
>
> On Tue, Sep 8, 2015 at 1:44 PM, Mark S. Miller <erig...@google.com> wrote:
>
>> See Labeler at
>> http://wiki.ecmascript.org/doku.php?id=harmony:weak_maps#unique_labeler
>>
>>
>>
>> On Tue, Sep 8, 2015 at 11:16 AM, joe <joe...@gmail.com> wrote:
>>
>>> Didn't send to list, something is wrong with my reply all. Sorry about
>>> that. Stupid mobile gmail.
>>> -- Forwarded message --
>>> From: "joe" <joe...@gmail.com>
>>> Date: Sep 8, 2015 11:15 AM
>>> Subject: Re: Object id, hash, etc?
>>> To: "Garrett Smith" <dhtmlkitc...@gmail.com>
>>> Cc:
>>>
>>> I agree with this request. This is the logical complement to valueof, I
>>> think. And yes, for most ID use cases this isn't a good fit, but we're not
>>> talking about the general case, just the cases where a python style id()
>>> function *is* appropriate.
>>>
>>> Joe
>>> On Sep 8, 2015 9:08 AM, "Garrett Smith" <dhtmlkitc...@gmail.com> wrote:
>>>
>>>> On 9/8/15, Michael McGlothlin <mike.mcgloth...@gmail.com> wrote:
>>>> > Is there a reason not to provide an object id and hash value as other
>>>> > languages often do? I find myself defining an identity symbol on
>>>> objects
>>>> > with a value from a simple global counter. It makes it easier to
>>>> debug if I
>>>> > can just look at a log and see what objects and functions were active.
>>>> >
>>>> > Likewise a hash value would simplify a lot of comparisons and make it
>>>> easier
>>>> > to debug.
>>>> >
>>>> > I especially find the ID useful when working with bound functions as
>>>> they
>>>> > can be difficult to tell apart. I like to change toString() to
>>>> provide the
>>>> > ID followed by the code (flattened to one line).
>>>> >
>>>> >
>>>>
>>>> NFE's are safe to use where IE8 support isn't needed*.
>>>> (function aa(){}).name; // "aa"
>>>>
>>>> As for using Object IDs and Object Pooling, lexically-scoped values
>>>> have benefits over global ID generators (as your counter). The values
>>>> can be passed in as a parameter to the Factory (useful with Private
>>>> Proxy). There other benefits to this pattern regarding memory
>>>> management and application design. Contact me if you'd like to learn
>>>> more.
>>>>
>>>> * https://kangax.github.io/nfe/
>>>> --
>>>> Garrett
>>>> @xkit
>>>> ChordCycles.wordpress.com
>>>> garretts.github.io
>>>> personx.tumblr.com
>>>> ___
>>>> es-discuss mailing list
>>>> es-discuss@mozilla.org
>>>> https://mail.mozilla.org/listinfo/es-discuss
>>>>
>>>
>>> ___
>>> es-discuss mailing list
>>> es-discuss@mozilla.org
>>> https://mail.mozilla.org/listinfo/es-discuss
>>>
>>>
>>
>>
>> --
>> Cheers,
>> --MarkM
>>
>> ___
>> es-discuss mailing list
>> es-discuss@mozilla.org
>> https://mail.mozilla.org/listinfo/es-discuss
>>
>>
>
> ___
> es-discuss mailing list
> es-discuss@mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss
>
>
___
es-discuss mailing list
es-discuss@mozilla.org
https://mail.mozilla.org/listinfo/es-discuss


Re: Object id, hash, etc?

2015-09-08 Thread Michael McGlothlin
I try to keep it pretty simple. It's not fancy but the times you want fast
and dirty information like this are the same times you don't want to have
to define it manually.

 Symbol.identity = Symbol( 'Symbol.identity' );
 const identity = Symbol( 'identity' );
 var OBJECT_ID = 0;
 Object.defineProperty( Object.prototype, Symbol.identity, {
  get: () => {
   if ( !Object.hasOwnProperty.call( this, identity ) ) {
this[ identity ] = ++OBJECT_ID;
   }
   return this[ identity ];
  }
 } );

On Tue, Sep 8, 2015 at 1:44 PM, Mark S. Miller <erig...@google.com> wrote:

> See Labeler at
> http://wiki.ecmascript.org/doku.php?id=harmony:weak_maps#unique_labeler
>
>
>
> On Tue, Sep 8, 2015 at 11:16 AM, joe <joe...@gmail.com> wrote:
>
>> Didn't send to list, something is wrong with my reply all. Sorry about
>> that. Stupid mobile gmail.
>> -- Forwarded message --
>> From: "joe" <joe...@gmail.com>
>> Date: Sep 8, 2015 11:15 AM
>> Subject: Re: Object id, hash, etc?
>> To: "Garrett Smith" <dhtmlkitc...@gmail.com>
>> Cc:
>>
>> I agree with this request. This is the logical complement to valueof, I
>> think. And yes, for most ID use cases this isn't a good fit, but we're not
>> talking about the general case, just the cases where a python style id()
>> function *is* appropriate.
>>
>> Joe
>> On Sep 8, 2015 9:08 AM, "Garrett Smith" <dhtmlkitc...@gmail.com> wrote:
>>
>>> On 9/8/15, Michael McGlothlin <mike.mcgloth...@gmail.com> wrote:
>>> > Is there a reason not to provide an object id and hash value as other
>>> > languages often do? I find myself defining an identity symbol on
>>> objects
>>> > with a value from a simple global counter. It makes it easier to debug
>>> if I
>>> > can just look at a log and see what objects and functions were active.
>>> >
>>> > Likewise a hash value would simplify a lot of comparisons and make it
>>> easier
>>> > to debug.
>>> >
>>> > I especially find the ID useful when working with bound functions as
>>> they
>>> > can be difficult to tell apart. I like to change toString() to provide
>>> the
>>> > ID followed by the code (flattened to one line).
>>> >
>>> >
>>>
>>> NFE's are safe to use where IE8 support isn't needed*.
>>> (function aa(){}).name; // "aa"
>>>
>>> As for using Object IDs and Object Pooling, lexically-scoped values
>>> have benefits over global ID generators (as your counter). The values
>>> can be passed in as a parameter to the Factory (useful with Private
>>> Proxy). There other benefits to this pattern regarding memory
>>> management and application design. Contact me if you'd like to learn
>>> more.
>>>
>>> * https://kangax.github.io/nfe/
>>> --
>>> Garrett
>>> @xkit
>>> ChordCycles.wordpress.com
>>> garretts.github.io
>>> personx.tumblr.com
>>> ___
>>> es-discuss mailing list
>>> es-discuss@mozilla.org
>>> https://mail.mozilla.org/listinfo/es-discuss
>>>
>>
>> ___
>> es-discuss mailing list
>> es-discuss@mozilla.org
>> https://mail.mozilla.org/listinfo/es-discuss
>>
>>
>
>
> --
> Cheers,
> --MarkM
>
> ___
> es-discuss mailing list
> es-discuss@mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss
>
>
___
es-discuss mailing list
es-discuss@mozilla.org
https://mail.mozilla.org/listinfo/es-discuss


RE: Object id, hash, etc?

2015-09-08 Thread Gary Guo
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.
  ___
es-discuss mailing list
es-discuss@mozilla.org
https://mail.mozilla.org/listinfo/es-discuss


Object id, hash, etc?

2015-09-08 Thread Michael McGlothlin
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@mozilla.org
https://mail.mozilla.org/listinfo/es-discuss


Re: Object id, hash, etc?

2015-09-08 Thread joe
Didn't send to list, something is wrong with my reply all. Sorry about
that. Stupid mobile gmail.
-- Forwarded message --
From: "joe" <joe...@gmail.com>
Date: Sep 8, 2015 11:15 AM
Subject: Re: Object id, hash, etc?
To: "Garrett Smith" <dhtmlkitc...@gmail.com>
Cc:

I agree with this request. This is the logical complement to valueof, I
think. And yes, for most ID use cases this isn't a good fit, but we're not
talking about the general case, just the cases where a python style id()
function *is* appropriate.

Joe
On Sep 8, 2015 9:08 AM, "Garrett Smith" <dhtmlkitc...@gmail.com> wrote:

> On 9/8/15, Michael McGlothlin <mike.mcgloth...@gmail.com> wrote:
> > Is there a reason not to provide an object id and hash value as other
> > languages often do? I find myself defining an identity symbol on objects
> > with a value from a simple global counter. It makes it easier to debug
> if I
> > can just look at a log and see what objects and functions were active.
> >
> > Likewise a hash value would simplify a lot of comparisons and make it
> easier
> > to debug.
> >
> > I especially find the ID useful when working with bound functions as they
> > can be difficult to tell apart. I like to change toString() to provide
> the
> > ID followed by the code (flattened to one line).
> >
> >
>
> NFE's are safe to use where IE8 support isn't needed*.
> (function aa(){}).name; // "aa"
>
> As for using Object IDs and Object Pooling, lexically-scoped values
> have benefits over global ID generators (as your counter). The values
> can be passed in as a parameter to the Factory (useful with Private
> Proxy). There other benefits to this pattern regarding memory
> management and application design. Contact me if you'd like to learn
> more.
>
> * https://kangax.github.io/nfe/
> --
> Garrett
> @xkit
> ChordCycles.wordpress.com
> garretts.github.io
> personx.tumblr.com
> ___
> es-discuss mailing list
> es-discuss@mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss
>
___
es-discuss mailing list
es-discuss@mozilla.org
https://mail.mozilla.org/listinfo/es-discuss