Skip to content
This repository has been archived by the owner on Jan 25, 2022. It is now read-only.

Spread operator copies Symbol property from one to another object #61

Open
Konrud opened this issue Feb 7, 2018 · 5 comments
Open

Spread operator copies Symbol property from one to another object #61

Konrud opened this issue Feb 7, 2018 · 5 comments

Comments

@Konrud
Copy link

Konrud commented Feb 7, 2018

Current implementation of the spread operator in browsers copy Symbol property when used.

What I see here is inconsistency.
On the one hand spread operator and Object.assign both do copy Symbol property.
On the other hand, Symbol property doesn't appear in for...in loop and completely ignored by JSON.stringify and JSON.parse.

Steps to reproduce the problem:

  1. Define object on which Symbol will be used as property name (e.g. obj[Symbol("secret property")] = "secret value")
  2. Using spread operator copy all properties to another object (e.g. const newObj = {...obj})

Spread operator should copy only own enumerable (which is not relevant to the property named using Symbol as it's not enumerable) properties from a provided object onto a new object.

Checked in:
Chrome version: 63.0.3239.132
(Windows 7)

working example: https://goo.gl/SkHd3H

@loganfsmyth
Copy link

Spread operator should copy only own enumerable

That is exactly what it is doing.

(which is not relevant to the property named using Symbol as it's not enumerable)

That is not the case, Symbol properties may or may not be enumerable, just like any other property.

On the other hand, Symbol property doesn't appear in for...in loop and completely ignored by JSON.stringify and JSON.parse.

What is leading to your confusion is that for..in and JSON.stringify (and Object.keys and such) don't simply iterate own enumerable properties, it is explicitly specced to iterate and only process string-keyed properties of the given object.

@gabssnake
Copy link

This seems completely rational, yet in practice, kinda sucks anyway

@LinusU
Copy link

LinusU commented Aug 6, 2021

yet in practice, kinda sucks anyway

Why? I think it would be more weird if it didn't copy symbols? 🤔

@gabssnake
Copy link

@LinusU well, if you're using plain objects as dictionaries, you'd only want stringy-key entries. I'd argue that this is a very common use case.

@LinusU
Copy link

LinusU commented Aug 7, 2021

Well, if you do that, then they wouldn't have any symbols on them that would get copied over? 🤔

Sorry, I'm not trying to argue with you, I'm just trying to understand your use case

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants