Notes about 'Not a duplicate':
I've been told this is a duplicate of What is the use of Symbol in javascript ECMAScript 6?. Well, it doesn't seem right to me. The code they've given is this:
const door = {};
// library 1
const cake1 = Symbol('cake');
door[cake1] = () => console.log('chocolate');
// library 2
const cake2 = Symbol('cake');
door[cake2] = () => console.log('vanilla');
// your code
door[cake1]();
door[cake2]();
The only thing that makes this work is because cake1
and cake2
are different (unique) names. But the developer has explicitly given these; there is nothing offered by Symbol
which helps here.
For example if you change cake1
and cake2
to cake
and run it, it will error:
Uncaught SyntaxError: Identifier 'cake' has already been declared
If you're already having to manually come up with unique identifiers then how is Symbol
helping?
If you execute this in your console:
Symbol('cake') === Symbol('cake');
It evaluates to false
. So they're unique. But in order to actually use them, you're now having to come up with 2 key names (cake1
and cake2
) which are unique. This has to be done manually by the developer; there's nothing in Symbol
or JavaScript in general which will help with that. You're basically creating a unique identifier using Symbol
but then having to assign it manually to...a unique identifier that you've had to come up with as a developer.
With regards to the linked post they cite this as an example which does not use Symbol
:
const door = {};
// from library 1
door.cake = () => console.log('chocolate');
// from library 2
door.cake = () => console.log('vanilla');
// your code
door.cake();
They try to claim this is a problem and will only log "vanilla". Well clearly that's because door.cake
isn't unique (it's declared twice). The "fix" is as simple as using cake1
and cake2
:
door.cake1 = () => console.log('chocolate');
door.cake2 = () => console.log('vanilla');
door.cake1(); // Outputs "chocolate"
door.cake2(); // Outputs "vanilla"
That will now work and log both "chocolate" and "vanilla". In this case Symbol
hasn't been used at all, and indeed has no bearing on that working. It's simply a case that the developer has assigned a unique identifier but they have done this manually and without using Symbol
.
Original question:
I'm taking a course in JavaScript and the presenter is discussing Symbol
.
At the beginning of the video he says:
The thing about Symbol's is that every single one is unique and this makes them very valuable in terms of things like object property identifiers.
However he then goes on to say:
- They are not enumerable in
for...in
loops.- They cannot be used in
JSON.stringify
. (It results in an empty object).
In the case of point (2) he gives this example:
console.log(JSON.stringify({key: 'prop'})); // object without Symbol
console.log(JSON.stringify({Symbol('sym1'): 'prop'})); // object using Symbol
This logs {"key": "prop"}
and {}
to the console respectively.
How does any of this make Symbol
"valuable" in terms of being unique object keys or identifiers?
In my experience two very common things you'd want to do with an object is enumerate it, or convert the data in them to JSON to send via ajax or some such method.
I can't understand what the purpose of Symbol
is at all, but especially why you would want to use them for making object identifiers? Given it will cause things later that you cannot do.
Edit - the following was part of the original question - but is a minor issue in comparison to the actual purpose of Symbol
with respect to unique identifiers:
If you needed to send something like {Symbol('sym1'): 'prop'}
to a backend via ajax what would you actually need to do in this case?