Closed MarHoff closed 6 years ago
Also to wrap up the last example should be something along the line of the following code:
const MySymbol = Symbol("My unique Symbol");
let stringObject = {
[MySymbol]() { return "something"; }
};
console.log(stringObject[MySymbol]());
// → something
And also this work but i have no idea why. Does it implicitly create a symbol as well?
Array.prototype[toString] = function() {
return `${this.length} cm of blue yarn`;
};
console.log([1, 2].toString());
// → 1,2
console.log([1, 2][toString]());
// → 2 cm of blue yarn
Attached patch tries to clarify this section a little.
Thanks a lot for all the work and sorry for late comment about it ;)
Hello, and first of thanks a lot for this good job,
I also apologize because I'm writing this issue while learning, maybe this matter would have become self evident at the end of the chapter.
I have difficulties coping with this concept of
Symbols
and the provided examples don't lift the fog as well as they might:I really don't understand in which context symbols are unique. But in my understanding Symbol() is a constructor for a 'symbol' object that is indeed unique as an object. You said later that
Maybe you could make the example more clear by using a string differentiated from the let binding name. And then use some computation on the symbol object.
Later I had same difficulties understanding the added value of using symbol with example code after this paragraph:
However this will perfectly work as well as name you choose avoided any collision:
So it would be easy to dismiss the concept altogether why bother? The following code example might lead to more head scratching but might also invite reader to more reflexion :
And a far as I understand now what actually guarantee that a symbol is unique is the ability to use a 'const' binding when defining it.