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/01-object/article.md
+2-2
Original file line number
Diff line number
Diff line change
@@ -322,7 +322,7 @@ alert( *!*key*/!* in user ); // true, takes the name from key and checks for suc
322
322
```
323
323
324
324
````smart header="Using \"in\" for properties that store `undefined`"
325
-
Usually, the strict comparison `"=== undefined"` check works fine. But there's a special case when it fails, but `"in"` works correctly.
325
+
Usually, the strict comparison `"=== undefined"` check the property existance just fine. But there's a special case when it fails, but `"in"` works correctly.
326
326
327
327
It's when an object property exists, but stores `undefined`:
328
328
@@ -569,7 +569,7 @@ user.age = 25; // (*)
569
569
alert(user.age); // 25
570
570
```
571
571
572
-
It might seem that the line `(*)` would cause an error, but no, there's totally no problem. That's because `const` fixes the value of`user`itself. And here `user` stores the reference to the same object all the time. The line `(*)` goes *inside* the object, it doesn't reassign `user`.
572
+
It might seem that the line `(*)` would cause an error, but no, there's totally no problem. That's because `const` fixes only value of`user`itself. And here `user` stores the reference to the same object all the time. The line `(*)` goes *inside* the object, it doesn't reassign `user`.
573
573
574
574
The `const` would give an error if we try to set `user` to something else, for instance:
As we've seen, usually all symbols are different, even if they have the same name. But sometimes we want same-named symbols to be same entities.
196
-
197
-
For instance, different parts of our application want to access symbol `"id"` meaning exactly the same property.
195
+
As we've seen, usually all symbols are different, even if they have the same name. But sometimes we want same-named symbols to be same entities. For instance, different parts of our application want to access symbol `"id"` meaning exactly the same property.
198
196
199
197
To achieve that, there exists a *global symbol registry*. We can create symbols in it and access them later, and it guarantees that repeated accesses by the same name return exactly the same symbol.
200
198
@@ -230,22 +228,29 @@ For global symbols, not only `Symbol.for(key)` returns a symbol by name, but the
230
228
For instance:
231
229
232
230
```js run
231
+
// get symbol by name
233
232
let sym =Symbol.for("name");
234
233
let sym2 =Symbol.for("id");
235
234
236
-
// get name from symbol
235
+
// get name by symbol
237
236
alert( Symbol.keyFor(sym) ); // name
238
237
alert( Symbol.keyFor(sym2) ); // id
239
238
```
240
239
241
240
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 return `undefined`.
242
241
242
+
That said, any symbols have `description` property.
243
+
243
244
For instance:
244
245
245
246
```js run
246
-
alert( Symbol.keyFor(Symbol.for("name")) ); // name, global symbol
247
+
let globalSymbol =Symbol.for("name");
248
+
let localSymbol =Symbol("name");
249
+
250
+
alert( Symbol.keyFor(globalSymbol) ); // name, global symbol
251
+
alert( Symbol.keyFor(localSymbol) ); // undefined, not global
247
252
248
-
alert( Symbol.keyFor(Symbol("name2")) ); //undefined, the argument isn't a global symbol
253
+
alert( localSymbol.description ); //name
249
254
```
250
255
251
256
## System symbols
@@ -281,4 +286,4 @@ Symbols have two main use cases:
281
286
282
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.
283
288
284
-
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 methods and syntax constructs adhere to a common agreement that they are. And the one who explicitly calls the aforementioned methods probably understands well what he's doing.
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.
Copy file name to clipboardExpand all lines: 1-js/04-object-basics/05-object-toprimitive/article.md
+13-26
Original file line number
Diff line number
Diff line change
@@ -15,9 +15,7 @@ In the chapter <info:type-conversions> we've seen the rules for numeric, string
15
15
16
16
We can fine-tune string and numeric conversion, using special object methods.
17
17
18
-
The conversion algorithm is called `ToPrimitive` in the [specification](https://tc39.github.io/ecma262/#sec-toprimitive). It's called with a "hint" that specifies the conversion type.
19
-
20
-
There are three variants:
18
+
There are three variants of type conversion, so-called "hints", described in the [specification](https://tc39.github.io/ecma262/#sec-toprimitive):
21
19
22
20
`"string"`
23
21
: For an object-to-string conversion, when we're doing an operation on an object that expects a string, like `alert`:
@@ -66,7 +64,7 @@ Please note -- there are only three hints. It's that simple. There is no "boolea
66
64
67
65
**To do the conversion, JavaScript tries to find and call three object methods:**
68
66
69
-
1. Call `obj[Symbol.toPrimitive](hint)`if the method exists,
67
+
1. Call `obj[Symbol.toPrimitive](hint)`- the method with the symbolic key `Symbol.toPrimitive` (system symbol), if such method exists,
70
68
2. Otherwise if hint is `"string"`
71
69
- try `obj.toString()` and `obj.valueOf()`, whatever exists.
72
70
3. Otherwise if hint is `"number"` or `"default"`
@@ -78,9 +76,9 @@ Let's start from the first method. There's a built-in symbol named `Symbol.toPri
Copy file name to clipboardExpand all lines: 1-js/05-data-types/01-primitives-methods/1-string-new-property/solution.md
+1-3
Original file line number
Diff line number
Diff line change
@@ -17,9 +17,7 @@ Why? Let's replay what's happening at line `(*)`:
17
17
18
18
1. When a property of `str` is accessed, a "wrapper object" is created.
19
19
2. In strict mode, writing into it is an error.
20
-
3. Otherwise, the operation with the property is carried on, the object gets the `test` property, but after that the "wrapper object" disappears.
21
-
22
-
So, without strict mode, in the last line `str` has no trace of the property.
20
+
3. Otherwise, the operation with the property is carried on, the object gets the `test` property, but after that the "wrapper object" disappears, so in the last line `str` has no trace of the property.
23
21
24
22
**This example clearly shows that primitives are not objects.**
Copy file name to clipboardExpand all lines: 1-js/05-data-types/01-primitives-methods/article.md
+3-5
Original file line number
Diff line number
Diff line change
@@ -1,8 +1,6 @@
1
1
# Methods of primitives
2
2
3
-
JavaScript allows us to work with primitives (strings, numbers, etc.) as if they were objects.
4
-
5
-
They also provide methods to call as such. We will study those soon, but first we'll see how it works because, of course, primitives are not objects (and here we will make it even clearer).
3
+
JavaScript allows us to work with primitives (strings, numbers, etc.) as if they were objects. They also provide methods to call as such. We will study those soon, but first we'll see how it works because, of course, primitives are not objects (and here we will make it even clearer).
6
4
7
5
Let's look at the key distinctions between primitives and objects.
8
6
@@ -35,7 +33,7 @@ Many built-in objects already exist, such as those that work with dates, errors,
35
33
36
34
But, these features come with a cost!
37
35
38
-
Objects are "heavier" than primitives. They require additional resources to support the internal machinery. But as properties and methods are very useful in programming, JavaScript engines try to optimize them to reduce the additional burden.
36
+
Objects are "heavier" than primitives. They require additional resources to support the internal machinery.
39
37
40
38
## A primitive as an object
41
39
@@ -84,7 +82,7 @@ We'll see more specific methods in chapters <info:number> and <info:string>.
84
82
85
83
86
84
````warn header="Constructors `String/Number/Boolean` are for internal use only"
87
-
Some languages like Java allow us to create "wrapper objects" for primitives explicitly using a syntax like `new Number(1)` or `new Boolean(false)`.
85
+
Some languages like Java allow us to explicitly create "wrapper objects" for primitives using a syntax like `new Number(1)` or `new Boolean(false)`.
88
86
89
87
In JavaScript, that's also possible for historical reasons, but highly **unrecommended**. Things will go crazy in several places.
Copy file name to clipboardExpand all lines: 1-js/05-data-types/02-number/article.md
+2-2
Original file line number
Diff line number
Diff line change
@@ -2,7 +2,7 @@
2
2
3
3
All numbers in JavaScript are stored in 64-bit format [IEEE-754](https://en.wikipedia.org/wiki/IEEE_754-2008_revision), also known as "double precision floating point numbers".
4
4
5
-
Let's recap and expand upon what we currently know about them.
5
+
Let's expand upon what we currently know about them.
6
6
7
7
## More ways to write a number
8
8
@@ -213,7 +213,7 @@ So, division by powers `10` is guaranteed to work well in the decimal system, bu
213
213
214
214
There's just no way to store *exactly 0.1* or *exactly 0.2* using the binary system, just like there is no way to store one-third as a decimal fraction.
215
215
216
-
The numeric format IEEE-754 solves this by rounding to the nearest possible number. These rounding rules normally don't allow us to see that "tiny precision loss", so the number shows up as `0.3`. But beware, the loss still exists.
216
+
The numeric format IEEE-754 solves this by rounding to the nearest possible number. These rounding rules normally don't allow us to see that "tiny precision loss", but it exists.
0 commit comments