Skip to content

Commit ec17aef

Browse files
committed
Update "Symbol type" article
1 parent fc20599 commit ec17aef

File tree

1 file changed

+24
-12
lines changed

1 file changed

+24
-12
lines changed

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

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
11

22
# Symbol type
33

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:
55

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.
714

815
## Symbols
916

@@ -12,18 +19,17 @@ A "symbol" represents a unique identifier.
1219
A value of this type can be created using `Symbol()`:
1320

1421
```js
15-
// id is a new symbol
1622
let id = Symbol();
1723
```
1824

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:
2026

2127
```js
2228
// id is a symbol with the description "id"
2329
let id = Symbol("id");
2430
```
2531

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.
2733

2834
For instance, here are two symbols with the same description -- they are not equal:
2935

@@ -38,6 +44,8 @@ alert(id1 == id2); // false
3844

3945
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.
4046

47+
So, to summarize, a symbol is a "primitive unique value" with an optional description. Let's see where we can use them.
48+
4149
````warn header="Symbols don't auto-convert to a string"
4250
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.
4351
@@ -53,6 +61,7 @@ alert(id); // TypeError: Cannot convert a Symbol value to a string
5361
That's a "language guard" against messing up, because strings and symbols are fundamentally different and should not accidentally convert one into another.
5462
5563
If we really want to show a symbol, we need to explicitly call `.toString()` on it, like here:
64+
5665
```js run
5766
let id = Symbol("id");
5867
*!*
@@ -61,6 +70,7 @@ alert(id.toString()); // Symbol(id), now it works
6170
```
6271
6372
Or get `symbol.description` property to show the description only:
73+
6474
```js run
6575
let id = Symbol("id");
6676
*!*
@@ -72,6 +82,7 @@ alert(id.description); // id
7282

7383
## "Hidden" properties
7484

85+
7586
Symbols allow us to create "hidden" properties of an object, that no other part of code can accidentally access or overwrite.
7687

7788
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
92103

93104
What's the benefit of using `Symbol("id")` over a string `"id"`?
94105

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.
96107

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.
98109

99110
Then that script can create its own `Symbol("id")`, like this:
100111

@@ -158,7 +169,7 @@ for (let key in user) alert(key); // name, age (no symbols)
158169
*/!*
159170

160171
// the direct access by the symbol works
161-
alert( "Direct: " + user[id] );
172+
alert( "Direct: " + user[id] ); // Direct: 123
162173
```
163174

164175
[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
206217
```smart header="That sounds like Ruby"
207218
In some programming languages, like Ruby, there's a single symbol per name.
208219
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.
210221
```
211222

212223
### Symbol.keyFor
213224

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)`:
215226

216227
For instance:
217228

@@ -227,7 +238,7 @@ alert( Symbol.keyFor(sym2) ); // id
227238

228239
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`.
229240

230-
That said, any symbols have `description` property.
241+
That said, all symbols have the `description` property.
231242

232243
For instance:
233244

@@ -268,10 +279,11 @@ Symbols are always different values, even if they have the same name. If we want
268279
Symbols have two main use cases:
269280

270281
1. "Hidden" object properties.
282+
271283
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.
272284

273285
So we can "covertly" hide something into objects that we need, but others should not see, using symbolic properties.
274286

275287
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.
276288

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

Comments
 (0)