Skip to content

Commit 2281506

Browse files
committed
integrates #2996
1 parent 89684c6 commit 2281506

File tree

1 file changed

+9
-9
lines changed

1 file changed

+9
-9
lines changed

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

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ By specification, only two primitive types may serve as object property keys:
88

99
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"]`.
1010

11-
Till now we've been using only strings.
11+
Until now we've been using only strings.
1212

1313
Now let's explore symbols, see what they can do for us.
1414

@@ -22,14 +22,14 @@ A value of this type can be created using `Symbol()`:
2222
let id = Symbol();
2323
```
2424

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

2727
```js
2828
// id is a symbol with the description "id"
2929
let id = Symbol("id");
3030
```
3131

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

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

@@ -44,7 +44,7 @@ alert(id1 == id2); // false
4444

4545
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.
4646

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

4949
````warn header="Symbols don't auto-convert to a string"
5050
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
103103

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

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

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

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

@@ -217,12 +217,12 @@ Symbols inside the registry are called *global symbols*. If we want an applicati
217217
```smart header="That sounds like Ruby"
218218
In some programming languages, like Ruby, there's a single symbol per name.
219219
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.
221221
```
222222

223223
### Symbol.keyFor
224224

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

227227
For instance:
228228

@@ -286,4 +286,4 @@ Symbols have two main use cases:
286286

287287
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.
288288

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

Comments
 (0)