This commit is contained in:
Ilya Kantor 2017-06-04 01:14:41 +03:00
parent c90377d996
commit 3cd2bcd48c
3 changed files with 29 additions and 5 deletions

View file

@ -33,6 +33,30 @@ alert(id1 == id2); // false
If you are familiar with Ruby or another language that also has some sort of "symbols" -- please don't be misguided. JavaScript symbols are different. If you are familiar with Ruby or another language that also has some sort of "symbols" -- please don't be misguided. JavaScript symbols are different.
````warn header="Symbols don't auto-convert to a string"
Most values in JavaScript support implicit conversion to a string. For instance, we can `alert` almost any value, and it will work. Symbols are special. They don't auto-convert.
For instance, this `alert` will show an error:
```js run
let id = Symbol("id");
*!*
alert(id); // TypeError: Cannot convert a Symbol value to a string
*/!*
```
If we really want to show a symbol, we need to call `.toString()` on it, like here:
```js run
let id = Symbol("id");
*!*
alert(id.toString()); // Symbol(id), now it works
*/!*
```
That's a "language guard" against messing up, because strings and symbols are fundamentally different and should not occasionally convert one into another.
````
## "Hidden" properties ## "Hidden" properties
@ -95,7 +119,7 @@ let user = {
``` ```
That's because we need the value from the variable `id` as the key, not the string "id". That's because we need the value from the variable `id` as the key, not the string "id".
### Symbols skipped by for..in ### Symbols are skipped by for..in
Symbolic properties do not participate in `for..in` loop. Symbolic properties do not participate in `for..in` loop.
@ -132,7 +156,7 @@ let clone = Object.assign({}, user);
alert( clone[id] ); // 123 alert( clone[id] ); // 123
``` ```
There's no paradox here. That's by design. The idea is that when we clone an object or merge objects, we usually want symbolic properties (like `id`) to be copied as well. There's no paradox here. That's by design. The idea is that when we clone an object or merge objects, we usually want *all* properties to be copied (including symbols like `id`).
````smart header="Property keys of other types are coerced to strings" ````smart header="Property keys of other types are coerced to strings"
We can only use strings or symbols as keys in objects. Other types are coerced to strings. We can only use strings or symbols as keys in objects. Other types are coerced to strings.

View file

@ -211,7 +211,7 @@ For instance:
``` ```
```smart header="Historical notes" ```smart header="Historical notes"
For historical reasons, methods `toString` or `valueOf` *should* return a primitive: if any of them returns an object, then there's no error, but the object is ignored (like if the method didn't exist). For historical reasons, methods `toString` or `valueOf` *should* return a primitive: if any of them returns an object, then there's no error, but that object is ignored (like if the method didn't exist).
In contrast, `Symbol.toPrimitive` *must* return an primitive, otherwise there will be an error. In contrast, `Symbol.toPrimitive` *must* return an primitive, otherwise there will be an error.
``` ```

View file

@ -719,6 +719,6 @@ For the full list, see the [manual](mdn:js/Array).
From the first sight it may seem that there are so many methods, quite difficult to remember. But actually that's much easier than it seems. From the first sight it may seem that there are so many methods, quite difficult to remember. But actually that's much easier than it seems.
Look through the cheatsheet just to be aware of them. Then do the tasks of this chapter to practice, so that you have experience with most methods. Look through the cheatsheet just to be aware of them. Then solve the tasks of this chapter to practice, so that you have experience with array methods.
Afterwards whether you need to do something with an array, and you don't know how -- come here, look at the cheatsheet and find the right method. Examples will help you to write it correctly. Soon you'll automatically remember the methods, without specific efforts from your side. Afterwards whenever you need to do something with an array, and you don't know how -- come here, look at the cheatsheet and find the right method. Examples will help you to write it correctly. Soon you'll automatically remember the methods, without specific efforts from your side.