Skip to content

Commit 34e9cdc

Browse files
committed
minor
1 parent 3ba28aa commit 34e9cdc

File tree

10 files changed

+55
-62
lines changed

10 files changed

+55
-62
lines changed

‎1-js/04-object-basics/01-object/article.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,7 @@ alert( *!*key*/!* in user ); // true, takes the name from key and checks for suc
322322
```
323323
324324
````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.
326326
327327
It's when an object property exists, but stores `undefined`:
328328
@@ -569,7 +569,7 @@ user.age = 25; // (*)
569569
alert(user.age); // 25
570570
```
571571

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`.
573573
574574
The `const` would give an error if we try to set `user` to something else, for instance:
575575

‎1-js/04-object-basics/03-symbol/article.md

+12-7
Original file line numberDiff line numberDiff line change
@@ -192,9 +192,7 @@ alert( obj[0] ); // test (same property)
192192

193193
## Global symbols
194194

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

199197
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.
200198

@@ -230,22 +228,29 @@ For global symbols, not only `Symbol.for(key)` returns a symbol by name, but the
230228
For instance:
231229

232230
```js run
231+
// get symbol by name
233232
let sym = Symbol.for("name");
234233
let sym2 = Symbol.for("id");
235234

236-
// get name from symbol
235+
// get name by symbol
237236
alert( Symbol.keyFor(sym) ); // name
238237
alert( Symbol.keyFor(sym2) ); // id
239238
```
240239

241240
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`.
242241

242+
That said, any symbols have `description` property.
243+
243244
For instance:
244245

245246
```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
247252

248-
alert( Symbol.keyFor(Symbol("name2")) ); // undefined, the argument isn't a global symbol
253+
alert( localSymbol.description ); // name
249254
```
250255

251256
## System symbols
@@ -281,4 +286,4 @@ Symbols have two main use cases:
281286

282287
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.
283288

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.

‎1-js/04-object-basics/04-object-methods/article.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ let user = {
111111

112112
sayHi() {
113113
*!*
114+
// "this" is the "current object"
114115
alert(this.name);
115116
*/!*
116117
}
@@ -176,7 +177,7 @@ function sayHi() {
176177
}
177178
```
178179

179-
The value of `this` is evaluated during the run-time, depending on the context. And it can be anything.
180+
The value of `this` is evaluated during the run-time, depending on the context.
180181

181182
For instance, here the same function is assigned to two different objects and has different "this" in the calls:
182183

‎1-js/04-object-basics/05-object-toprimitive/article.md

+13-26
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,7 @@ In the chapter <info:type-conversions> we've seen the rules for numeric, string
1515

1616
We can fine-tune string and numeric conversion, using special object methods.
1717

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):
2119

2220
`"string"`
2321
: 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
6664

6765
**To do the conversion, JavaScript tries to find and call three object methods:**
6866

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,
7068
2. Otherwise if hint is `"string"`
7169
- try `obj.toString()` and `obj.valueOf()`, whatever exists.
7270
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
7876

7977
```js
8078
obj[Symbol.toPrimitive] = function(hint) {
81-
// return a primitive value
79+
// must return a primitive value
8280
// hint = one of "string", "number", "default"
83-
}
81+
};
8482
```
8583

8684
For instance, here `user` object implements it:
@@ -138,6 +136,8 @@ alert(+user); // valueOf -> 1000
138136
alert(user + 500); // valueOf -> 1500
139137
```
140138

139+
As we can see, the behavior is the same as the previous example with `Symbol.toPrimitive`.
140+
141141
Often we want a single "catch-all" place to handle all primitive conversions. In this case, we can implement `toString` only, like this:
142142

143143
```js run
@@ -171,25 +171,24 @@ In contrast, `Symbol.toPrimitive` *must* return a primitive, otherwise there wil
171171

172172
## Further operations
173173

174-
An operation that initiated the conversion gets that primitive, and then continues to work with it, applying further conversions if necessary.
174+
An operation that initiated the conversion gets the primitive, and then continues to work with it, applying further conversions if necessary.
175175

176176
For instance:
177177

178-
- Mathematical operations (except binary plus) perform `ToNumber` conversion:
178+
- Mathematical operations, except binary plus, convert the primitive to a number:
179179

180180
```js run
181181
let obj = {
182-
toString() { // toString handles all conversions in the absence of other methods
182+
// toString handles all conversions in the absence of other methods
183+
toString() {
183184
return "2";
184185
}
185186
};
186187

187-
alert(obj * 2); // 4, ToPrimitive gives "2", then it becomes 2
188+
alert(obj * 2); // 4, object converted to primitive "2", then multiplication made it a number
188189
```
189190

190-
- Binary plus checks the primitive -- if it's a string, then it does concatenation, otherwise it performs `ToNumber` and works with numbers.
191-
192-
String example:
191+
- Binary plus will concatenate strings in the same situation:
193192
```js run
194193
let obj = {
195194
toString() {
@@ -200,24 +199,12 @@ For instance:
200199
alert(obj + 2); // 22 (ToPrimitive returned string => concatenation)
201200
```
202201

203-
Number example:
204-
```js run
205-
let obj = {
206-
toString() {
207-
return true;
208-
}
209-
};
210-
211-
alert(obj + 2); // 3 (ToPrimitive returned boolean, not string => ToNumber)
212-
```
213-
214-
215202
## Summary
216203

217204
The object-to-primitive conversion is called automatically by many built-in functions and operators that expect a primitive as a value.
218205

219206
There are 3 types (hints) of it:
220-
- `"string"` (for `alert` and other string conversions)
207+
- `"string"` (for `alert` and other operations that need a string)
221208
- `"number"` (for maths)
222209
- `"default"` (few operators)
223210

‎1-js/04-object-basics/06-constructor-new/3-accumulator/task.md

+2
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,10 @@ Here's the demo of the code:
1717

1818
```js
1919
let accumulator = new Accumulator(1); // initial value 1
20+
2021
accumulator.read(); // adds the user-entered value
2122
accumulator.read(); // adds the user-entered value
23+
2224
alert(accumulator.value); // shows the sum of these values
2325
```
2426

‎1-js/04-object-basics/06-constructor-new/article.md

+2
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,8 @@ john = {
215215
*/
216216
```
217217

218+
To create complex objects, there's a more advanced syntax, [classes](info:classes), that we'll cover later.
219+
218220
## Summary
219221

220222
- Constructor functions or, briefly, constructors, are regular functions, but there's a common agreement to name them with capital letter first.

‎1-js/05-data-types/01-primitives-methods/1-string-new-property/solution.md

+1-3
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,7 @@ Why? Let's replay what's happening at line `(*)`:
1717

1818
1. When a property of `str` is accessed, a "wrapper object" is created.
1919
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.
2321

2422
**This example clearly shows that primitives are not objects.**
2523

‎1-js/05-data-types/01-primitives-methods/article.md

+3-5
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
# Methods of primitives
22

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).
64

75
Let's look at the key distinctions between primitives and objects.
86

@@ -35,7 +33,7 @@ Many built-in objects already exist, such as those that work with dates, errors,
3533

3634
But, these features come with a cost!
3735

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

4038
## A primitive as an object
4139

@@ -84,7 +82,7 @@ We'll see more specific methods in chapters <info:number> and <info:string>.
8482

8583

8684
````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)`.
8886

8987
In JavaScript, that's also possible for historical reasons, but highly **unrecommended**. Things will go crazy in several places.
9088

‎1-js/05-data-types/02-number/article.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
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".
44

5-
Let's recap and expand upon what we currently know about them.
5+
Let's expand upon what we currently know about them.
66

77
## More ways to write a number
88

@@ -213,7 +213,7 @@ So, division by powers `10` is guaranteed to work well in the decimal system, bu
213213

214214
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.
215215

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

218218
We can see this in action:
219219
```js run

0 commit comments

Comments
 (0)