Object.equals() and Object.clone()

classic Classic list List threaded Threaded
9 messages Options
Reply | Threaded
Open this post in threaded view
|

Object.equals() and Object.clone()

Kevin Barabash
It would be nice if deep equality checking and deep cloning of objects was included in the standard library.  Has there been any proposals around including these in the past?

– Kevin


_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Object.equals() and Object.clone()

Frankie Bagnardi
It's pretty hard to decide how these behave, specifically with custom classes. Focusing on Object.clone...

- with classes do you call the constructor, and with what arguments?
- HTMLElement and sub classes can't be constructed directly, what happens with them?
- do you copy internal properties? this would make it hard to polyfill
- how does it behave with getters and setters?
- with regular expressions do you copy the lastIndex?

Most of those apply to Object.equals also.



On Mon, Nov 14, 2016 at 6:25 PM, Kevin Barabash <[hidden email]> wrote:
It would be nice if deep equality checking and deep cloning of objects was included in the standard library.  Has there been any proposals around including these in the past?

– Kevin


_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss



_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Object.equals() and Object.clone()

Isiah Meadows-2
I agree. Also, consider iterables. Should their `Symbol.iterator` method be called? There's no obvious behavior for the details, and people's opinions differ on what should be correct. 

On Mon, Nov 14, 2016, 20:58 Frankie Bagnardi <[hidden email]> wrote:
It's pretty hard to decide how these behave, specifically with custom classes. Focusing on Object.clone...

- with classes do you call the constructor, and with what arguments?
- HTMLElement and sub classes can't be constructed directly, what happens with them?
- do you copy internal properties? this would make it hard to polyfill
- how does it behave with getters and setters?
- with regular expressions do you copy the lastIndex?

Most of those apply to Object.equals also.



On Mon, Nov 14, 2016 at 6:25 PM, Kevin Barabash <[hidden email]> wrote:
It would be nice if deep equality checking and deep cloning of objects was included in the standard library.  Has there been any proposals around including these in the past?

– Kevin


_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss


_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss

_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Object.equals() and Object.clone()

Eric Devine
How about beginning with the HTML structured clone algorithm, and a `Symbol.clone` property to allow an object to optionally define it's own cloning behavior?

On Mon, Nov 14, 2016 at 9:02 PM, Isiah Meadows <[hidden email]> wrote:
I agree. Also, consider iterables. Should their `Symbol.iterator` method be called? There's no obvious behavior for the details, and people's opinions differ on what should be correct. 

On Mon, Nov 14, 2016, 20:58 Frankie Bagnardi <[hidden email]> wrote:
It's pretty hard to decide how these behave, specifically with custom classes. Focusing on Object.clone...

- with classes do you call the constructor, and with what arguments?
- HTMLElement and sub classes can't be constructed directly, what happens with them?
- do you copy internal properties? this would make it hard to polyfill
- how does it behave with getters and setters?
- with regular expressions do you copy the lastIndex?

Most of those apply to Object.equals also.



On Mon, Nov 14, 2016 at 6:25 PM, Kevin Barabash <[hidden email]> wrote:
It would be nice if deep equality checking and deep cloning of objects was included in the standard library.  Has there been any proposals around including these in the past?

– Kevin


_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss


_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss

_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss



_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Object.equals() and Object.clone()

Michael Theriot
In reply to this post by Frankie Bagnardi
I think you'd definitely need to call the constructor for classes because of scoped variables (e.g. scoped weakmap for private properties).

I would like a way to compare simple objects like identical arrays though.

On Mon, Nov 14, 2016 at 7:58 PM, Frankie Bagnardi <[hidden email]> wrote:
It's pretty hard to decide how these behave, specifically with custom classes. Focusing on Object.clone...

- with classes do you call the constructor, and with what arguments?
- HTMLElement and sub classes can't be constructed directly, what happens with them?
- do you copy internal properties? this would make it hard to polyfill
- how does it behave with getters and setters?
- with regular expressions do you copy the lastIndex?

Most of those apply to Object.equals also.



On Mon, Nov 14, 2016 at 6:25 PM, Kevin Barabash <[hidden email]> wrote:
It would be nice if deep equality checking and deep cloning of objects was included in the standard library.  Has there been any proposals around including these in the past?

– Kevin


_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss



_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss



_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Object.equals() and Object.clone()

Frankie Bagnardi
I guess it could throw if it's unable to clone something. This would include *any* class without `Symbol.clone`. The error messages would need to be outstanding for it to be practical. Like showing the path to the thing that can't be cloned and the name of the constructor.


On Mon, Nov 14, 2016 at 9:01 PM, Michael Theriot <[hidden email]> wrote:
I think you'd definitely need to call the constructor for classes because of scoped variables (e.g. scoped weakmap for private properties).

I would like a way to compare simple objects like identical arrays though.

On Mon, Nov 14, 2016 at 7:58 PM, Frankie Bagnardi <[hidden email]> wrote:
It's pretty hard to decide how these behave, specifically with custom classes. Focusing on Object.clone...

- with classes do you call the constructor, and with what arguments?
- HTMLElement and sub classes can't be constructed directly, what happens with them?
- do you copy internal properties? this would make it hard to polyfill
- how does it behave with getters and setters?
- with regular expressions do you copy the lastIndex?

Most of those apply to Object.equals also.



On Mon, Nov 14, 2016 at 6:25 PM, Kevin Barabash <[hidden email]> wrote:
It would be nice if deep equality checking and deep cloning of objects was included in the standard library.  Has there been any proposals around including these in the past?

– Kevin


_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss



_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss




_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Object.equals() and Object.clone()

Kevin Barabash
In reply to this post by Eric Devine
I didn't know about structured clone so I looked it up.  It looks pretty promising.  As for checking if two things are equal, maybe it makes sense for objects to define their own `.equals(other)` methods.


On Mon, Nov 14, 2016 at 9:31 PM, Eric Devine <[hidden email]> wrote:
How about beginning with the HTML structured clone algorithm, and a `Symbol.clone` property to allow an object to optionally define it's own cloning behavior?

On Mon, Nov 14, 2016 at 9:02 PM, Isiah Meadows <[hidden email]> wrote:
I agree. Also, consider iterables. Should their `Symbol.iterator` method be called? There's no obvious behavior for the details, and people's opinions differ on what should be correct. 

On Mon, Nov 14, 2016, 20:58 Frankie Bagnardi <[hidden email]> wrote:
It's pretty hard to decide how these behave, specifically with custom classes. Focusing on Object.clone...

- with classes do you call the constructor, and with what arguments?
- HTMLElement and sub classes can't be constructed directly, what happens with them?
- do you copy internal properties? this would make it hard to polyfill
- how does it behave with getters and setters?
- with regular expressions do you copy the lastIndex?

Most of those apply to Object.equals also.



On Mon, Nov 14, 2016 at 6:25 PM, Kevin Barabash <[hidden email]> wrote:
It would be nice if deep equality checking and deep cloning of objects was included in the standard library.  Has there been any proposals around including these in the past?

– Kevin


_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss


_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss

_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss




_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

RE: Object.equals() and Object.clone()

Domenic Denicola

The biggest problem with structured clone is that there’s no way to just invoke it directly. Doing so has been proposed a few times, most recently at https://github.com/whatwg/html/issues/793, but no implementers have expressed interest.

 

From: es-discuss [mailto:[hidden email]] On Behalf Of Kevin Barabash
Sent: Monday, November 14, 2016 23:51
To: Eric Devine <[hidden email]>
Cc: es-discuss <[hidden email]>
Subject: Re: Object.equals() and Object.clone()

 

I didn't know about structured clone so I looked it up.  It looks pretty promising.  As for checking if two things are equal, maybe it makes sense for objects to define their own `.equals(other)` methods.

 

 

On Mon, Nov 14, 2016 at 9:31 PM, Eric Devine <[hidden email]> wrote:

How about beginning with the HTML structured clone algorithm, and a `Symbol.clone` property to allow an object to optionally define it's own cloning behavior?

 

On Mon, Nov 14, 2016 at 9:02 PM, Isiah Meadows <[hidden email]> wrote:

I agree. Also, consider iterables. Should their `Symbol.iterator` method be called? There's no obvious behavior for the details, and people's opinions differ on what should be correct. 

 

On Mon, Nov 14, 2016, 20:58 Frankie Bagnardi <[hidden email]> wrote:

It's pretty hard to decide how these behave, specifically with custom classes. Focusing on Object.clone...

 

- with classes do you call the constructor, and with what arguments?

- HTMLElement and sub classes can't be constructed directly, what happens with them?

- do you copy internal properties? this would make it hard to polyfill

- how does it behave with getters and setters?

- with regular expressions do you copy the lastIndex?

 

Most of those apply to Object.equals also.

 

 

 

On Mon, Nov 14, 2016 at 6:25 PM, Kevin Barabash <[hidden email]> wrote:

It would be nice if deep equality checking and deep cloning of objects was included in the standard library.  Has there been any proposals around including these in the past?

 

– Kevin

 


_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss

 

_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss


_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss

 

 


_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Object.equals() and Object.clone()

Alexander Jones
In reply to this post by Kevin Barabash
Object has this unfortunate complication of prototypes, i.e.:

```
const objA = {foo: 1, bar: 2};
const objB = Object.create(objA);
equals(objA, objB) === ?
```

And also there is metadata on each property that can vary (one object has a property which is configurable, and the other has the same property which is not configurable), and raise similar questions.

Neither of these questions exist on ES's Map type, or Immutable.js's Map and List types, for example.

IMO we should be looking at ways to make those types more suitable for many of the use cases of Object.

On 15 November 2016 at 01:25, Kevin Barabash <[hidden email]> wrote:
It would be nice if deep equality checking and deep cloning of objects was included in the standard library.  Has there been any proposals around including these in the past?

– Kevin


_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss



_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss