You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: 1-js/04-object-basics/08-symbol/article.md
+24-12Lines changed: 24 additions & 12 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,9 +1,16 @@
1
1
2
2
# Symbol type
3
3
4
-
By specification, object property keys may be either of string type, or of symbol type. Not numbers, not booleans, only strings or symbols, these two types.
4
+
By specification, only two primitive types may serve as object property keys:
5
5
6
-
Till now we've been using only strings. Now let's see the benefits that symbols can give us.
6
+
- string type, or
7
+
- symbol type.
8
+
9
+
Otherwise, if one uses another type, such as number, it's autoconverted to string. So that `obj[1]` is the same as `obj["1"]`, and `obj[true]` is the same as `obj["true"]`.
10
+
11
+
Until now we've been using only strings.
12
+
13
+
Now let's explore symbols, see what they can do for us.
7
14
8
15
## Symbols
9
16
@@ -12,18 +19,17 @@ A "symbol" represents a unique identifier.
12
19
A value of this type can be created using `Symbol()`:
13
20
14
21
```js
15
-
// id is a new symbol
16
22
let id =Symbol();
17
23
```
18
24
19
-
Upon creation, we can give symbol a description (also called a symbol name), mostly useful for debugging purposes:
25
+
Upon creation, we can give symbols a description (also called a symbol name), mostly useful for debugging purposes:
20
26
21
27
```js
22
28
// id is a symbol with the description "id"
23
29
let id =Symbol("id");
24
30
```
25
31
26
-
Symbols are guaranteed to be unique. Even if we create many symbols with the same description, they are different values. The description is just a label that doesn't affect anything.
32
+
Symbols are guaranteed to be unique. Even if we create many symbols with exactly the same description, they are different values. The description is just a label that doesn't affect anything.
27
33
28
34
For instance, here are two symbols with the same description -- they are not equal:
29
35
@@ -38,6 +44,8 @@ alert(id1 == id2); // false
38
44
39
45
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.
40
46
47
+
So, to summarize, a symbol is a "primitive unique value" with an optional description. Let's see where we can use them.
48
+
41
49
````warn header="Symbols don't auto-convert to a string"
42
50
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.
43
51
@@ -53,6 +61,7 @@ alert(id); // TypeError: Cannot convert a Symbol value to a string
53
61
That's a "language guard" against messing up, because strings and symbols are fundamentally different and should not accidentally convert one into another.
54
62
55
63
If we really want to show a symbol, we need to explicitly call `.toString()` on it, like here:
64
+
56
65
```js run
57
66
let id = Symbol("id");
58
67
*!*
@@ -61,6 +70,7 @@ alert(id.toString()); // Symbol(id), now it works
61
70
```
62
71
63
72
Or get `symbol.description` property to show the description only:
73
+
64
74
```js run
65
75
let id = Symbol("id");
66
76
*!*
@@ -72,6 +82,7 @@ alert(id.description); // id
72
82
73
83
## "Hidden" properties
74
84
85
+
75
86
Symbols allow us to create "hidden" properties of an object, that no other part of code can accidentally access or overwrite.
76
87
77
88
For instance, if we're working with `user` objects, that belong to a third-party code. We'd like to add identifiers to them.
@@ -92,9 +103,9 @@ alert( user[id] ); // we can access the data using the symbol as the key
92
103
93
104
What's the benefit of using `Symbol("id")` over a string `"id"`?
94
105
95
-
As `user` objects belongs to another code, and that code also works with them, we shouldn't just add any fields to it. That's unsafe. But a symbol cannot be accessed accidentally, the third-party code probably won't even see it, so it's probably all right to do.
106
+
As `user` objects belong to another codebase, it's unsafe to add fields to them, since we might affect pre-defined behavior in that other codebase. However, symbols cannot be accessed accidentally. The third-party code won't be aware of newly defined symbols, so it's safe to add symbols to the `user` objects.
96
107
97
-
Also, imagine that another script wants to have its own identifier inside `user`, for its own purposes. That may be another JavaScript library, so that the scripts are completely unaware of each other.
108
+
Also, imagine that another script wants to have its own identifier inside `user`, for its own purposes.
98
109
99
110
Then that script can create its own `Symbol("id")`, like this:
100
111
@@ -158,7 +169,7 @@ for (let key in user) alert(key); // name, age (no symbols)
158
169
*/!*
159
170
160
171
// the direct access by the symbol works
161
-
alert( "Direct: "+ user[id] );
172
+
alert( "Direct: "+ user[id] );// Direct: 123
162
173
```
163
174
164
175
[Object.keys(user)](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/keys) also ignores them. That's a part of the general "hiding symbolic properties" principle. If another script or a library loops over our object, it won't unexpectedly access a symbolic property.
@@ -206,12 +217,12 @@ Symbols inside the registry are called *global symbols*. If we want an applicati
206
217
```smart header="That sounds like Ruby"
207
218
In some programming languages, like Ruby, there's a single symbol per name.
208
219
209
-
In JavaScript, as we can see, that's right for global symbols.
220
+
In JavaScript, as we can see, that's true for global symbols.
210
221
```
211
222
212
223
### Symbol.keyFor
213
224
214
-
For global symbols, not only `Symbol.for(key)` returns a symbol by name, but there's a reverse call: `Symbol.keyFor(sym)`, that does the reverse: returns a name by a global symbol.
225
+
We have seen that for global symbols, `Symbol.for(key)` returns a symbol by name. To do the opposite -- return a name by global symbol -- we can use: `Symbol.keyFor(sym)`:
215
226
216
227
For instance:
217
228
@@ -227,7 +238,7 @@ alert( Symbol.keyFor(sym2) ); // id
227
238
228
239
The `Symbol.keyFor` internally uses the global symbol registry to look up the key for the symbol. So it doesn't work for non-global symbols. If the symbol is not global, it won't be able to find it and returns `undefined`.
229
240
230
-
That said, any symbols have `description` property.
241
+
That said, all symbols have the`description` property.
231
242
232
243
For instance:
233
244
@@ -268,10 +279,11 @@ Symbols are always different values, even if they have the same name. If we want
268
279
Symbols have two main use cases:
269
280
270
281
1. "Hidden" object properties.
282
+
271
283
If we want to add a property into an object that "belongs" to another script or a library, we can create a symbol and use it as a property key. A symbolic property does not appear in `for..in`, so it won't be accidentally processed together with other properties. Also it won't be accessed directly, because another script does not have our symbol. So the property will be protected from accidental use or overwrite.
272
284
273
285
So we can "covertly" hide something into objects that we need, but others should not see, using symbolic properties.
274
286
275
287
2. There are many system symbols used by JavaScript which are accessible as `Symbol.*`. We can use them to alter some built-in behaviors. For instance, later in the tutorial we'll use `Symbol.iterator` for [iterables](info:iterable), `Symbol.toPrimitive` to setup [object-to-primitive conversion](info:object-toprimitive) and so on.
276
288
277
-
Technically, symbols are not 100% hidden. There is a built-in method [Object.getOwnPropertySymbols(obj)](mdn:js/Object/getOwnPropertySymbols) that allows us to get all symbols. Also there is a method named [Reflect.ownKeys(obj)](mdn:js/Reflect/ownKeys) that returns *all* keys of an object including symbolic ones. So they are not really hidden. But most libraries, built-in functions and syntax constructs don't use these methods.
289
+
Technically, symbols are not 100% hidden. There is a built-in method [Object.getOwnPropertySymbols(obj)](mdn:js/Object/getOwnPropertySymbols) that allows us to get all symbols. Also there is a method named [Reflect.ownKeys(obj)](mdn:js/Reflect/ownKeys) that returns *all* keys of an object including symbolic ones. But most libraries, built-in functions and syntax constructs don't use these methods.
0 commit comments