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/03-symbol/Change 'occasional' to 'accidental' where appropriate
+4-4
Original file line number
Diff line number
Diff line change
@@ -50,7 +50,7 @@ alert(id); // TypeError: Cannot convert a Symbol value to a string
50
50
*/!*
51
51
```
52
52
53
-
That's a "language guard" against messing up, because strings and symbols are fundamentally different and should not occasionally convert one into another.
53
+
That's a "language guard" against messing up, because strings and symbols are fundamentally different and should not accidentally convert one into another.
54
54
55
55
If we really want to show a symbol, we need to explicitly call `.toString()` on it, like here:
56
56
```js run
@@ -72,7 +72,7 @@ alert(id.description); // id
72
72
73
73
## "Hidden" properties
74
74
75
-
Symbols allow us to create "hidden" properties of an object, that no other part of code can occasionally access or overwrite.
75
+
Symbols allow us to create "hidden" properties of an object, that no other part of code can accidentally access or overwrite.
76
76
77
77
For instance, if we're working with `user` objects, that belong to a third-party code. We'd like to add identifiers to them.
78
78
@@ -92,7 +92,7 @@ alert( user[id] ); // we can access the data using the symbol as the key
92
92
93
93
What's the benefit of using `Symbol("id")` over a string `"id"`?
94
94
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 occasionally, the third-party code probably won't even see it, so it's probably all right to do.
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.
96
96
97
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.
98
98
@@ -284,7 +284,7 @@ Symbols are always different values, even if they have the same name. If we want
284
284
Symbols have two main use cases:
285
285
286
286
1. "Hidden" object properties.
287
-
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 occasionally 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 occasional use or overwrite.
287
+
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.
288
288
289
289
So we can "covertly" hide something into objects that we need, but others should not see, using symbolic properties.
0 commit comments