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
+9-9Lines changed: 9 additions & 9 deletions
Original file line number
Diff line number
Diff line change
@@ -8,7 +8,7 @@ By specification, only two primitive types may serve as object property keys:
8
8
9
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
10
11
-
Till now we've been using only strings.
11
+
Until now we've been using only strings.
12
12
13
13
Now let's explore symbols, see what they can do for us.
14
14
@@ -22,14 +22,14 @@ A value of this type can be created using `Symbol()`:
22
22
let id =Symbol();
23
23
```
24
24
25
-
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:
26
26
27
27
```js
28
28
// id is a symbol with the description "id"
29
29
let id =Symbol("id");
30
30
```
31
31
32
-
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.
33
33
34
34
For instance, here are two symbols with the same description -- they are not equal:
35
35
@@ -44,7 +44,7 @@ alert(id1 == id2); // false
44
44
45
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.
46
46
47
-
So, to summarize, symbols are "primitive unique values" with optional description. Let's see where we can use them.
47
+
So, to summarize, a symbol is a "primitive unique value" with an optional description. Let's see where we can use them.
48
48
49
49
````warn header="Symbols don't auto-convert to a string"
50
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.
@@ -103,9 +103,9 @@ alert( user[id] ); // we can access the data using the symbol as the key
103
103
104
104
What's the benefit of using `Symbol("id")` over a string `"id"`?
105
105
106
-
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.
107
107
108
-
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.
109
109
110
110
Then that script can create its own `Symbol("id")`, like this:
111
111
@@ -217,12 +217,12 @@ Symbols inside the registry are called *global symbols*. If we want an applicati
217
217
```smart header="That sounds like Ruby"
218
218
In some programming languages, like Ruby, there's a single symbol per name.
219
219
220
-
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.
221
221
```
222
222
223
223
### Symbol.keyFor
224
224
225
-
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)`:
226
226
227
227
For instance:
228
228
@@ -286,4 +286,4 @@ Symbols have two main use cases:
286
286
287
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.
288
288
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. 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