Skip to content

Commit 3cd2bcd

Browse files
committed
symbols
1 parent c90377d commit 3cd2bcd

File tree

3 files changed

+29
-5
lines changed

3 files changed

+29
-5
lines changed

1-js/04-object-basics/03-symbol/article.md

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,30 @@ alert(id1 == id2); // false
3333

3434
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.
3535

36+
````warn header="Symbols don't auto-convert to a string"
37+
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.
38+
39+
For instance, this `alert` will show an error:
40+
41+
```js run
42+
let id = Symbol("id");
43+
*!*
44+
alert(id); // TypeError: Cannot convert a Symbol value to a string
45+
*/!*
46+
```
47+
48+
If we really want to show a symbol, we need to call `.toString()` on it, like here:
49+
```js run
50+
let id = Symbol("id");
51+
*!*
52+
alert(id.toString()); // Symbol(id), now it works
53+
*/!*
54+
```
55+
56+
That's a "language guard" against messing up, because strings and symbols are fundamentally different and should not occasionally convert one into another.
57+
````
58+
59+
3660

3761
## "Hidden" properties
3862

@@ -95,7 +119,7 @@ let user = {
95119
```
96120
That's because we need the value from the variable `id` as the key, not the string "id".
97121

98-
### Symbols skipped by for..in
122+
### Symbols are skipped by for..in
99123

100124
Symbolic properties do not participate in `for..in` loop.
101125

@@ -132,7 +156,7 @@ let clone = Object.assign({}, user);
132156
alert( clone[id] ); // 123
133157
```
134158

135-
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.
159+
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`).
136160

137161
````smart header="Property keys of other types are coerced to strings"
138162
We can only use strings or symbols as keys in objects. Other types are coerced to strings.

1-js/04-object-basics/05-object-toprimitive/article.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ For instance:
211211
```
212212
213213
```smart header="Historical notes"
214-
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).
214+
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).
215215
216216
In contrast, `Symbol.toPrimitive` *must* return an primitive, otherwise there will be an error.
217217
```

1-js/05-data-types/05-array-methods/article.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -719,6 +719,6 @@ For the full list, see the [manual](mdn:js/Array).
719719

720720
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.
721721

722-
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.
722+
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.
723723

724-
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.
724+
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.

0 commit comments

Comments
 (0)