Skip to content

Commit 2c4cc64

Browse files
committed
up
1 parent a956c4f commit 2c4cc64

File tree

2 files changed

+27
-26
lines changed

2 files changed

+27
-26
lines changed

Diff for: 1-js/04-object-basics/05-object-toprimitive/article.md

+10-8
Original file line numberDiff line numberDiff line change
@@ -155,14 +155,21 @@ alert(user + 500); // toString -> John500
155155

156156
In the absence of `Symbol.toPrimitive` and `valueOf`, `toString` will handle all primitive conversions.
157157

158-
159-
## ToPrimitive and ToString/ToNumber
158+
## Return types
160159

161160
The important thing to know about all primitive-conversion methods is that they do not necessarily return the "hinted" primitive.
162161

163162
There is no control whether `toString()` returns exactly a string, or whether `Symbol.toPrimitive` method returns a number for a hint "number".
164163

165-
**The only mandatory thing: these methods must return a primitive.**
164+
The only mandatory thing: these methods must return a primitive, not an object.
165+
166+
```smart header="Historical notes"
167+
For historical reasons, if `toString` or `valueOf` return an object, there's no error, but such value is ignored (like if the method didn't exist). That's because in ancient times there was no good "error" concept in JavaScript.
168+
169+
In contrast, `Symbol.toPrimitive` *must* return a primitive, otherwise there will be an error.
170+
```
171+
172+
## Further operations
166173

167174
An operation that initiated the conversion gets that primitive, and then continues to work with it, applying further conversions if necessary.
168175

@@ -204,11 +211,6 @@ For instance:
204211
alert(obj + 2); // 3 (ToPrimitive returned boolean, not string => ToNumber)
205212
```
206213
207-
```smart header="Historical notes"
208-
For historical reasons, methods `toString` or `valueOf` *should* return a primitive: if any of them returns an object, then there's no error, but that object is ignored (like if the method didn't exist).
209-
210-
In contrast, `Symbol.toPrimitive` *must* return a primitive, otherwise, there will be an error.
211-
```
212214
213215
## Summary
214216

Diff for: 1-js/05-data-types/04-array/article.md

+17-18
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
# Arrays
1+
# Arrays
22

33
Objects allow you to store keyed collections of values. That's fine.
44

5-
But quite often we find that we need an *ordered collection*, where we have a 1st, a 2nd, a 3rd element and so on. For example, we need that to store a list of something: users, goods, HTML elements etc.
5+
But quite often we find that we need an *ordered collection*, where we have a 1st, a 2nd, a 3rd element and so on. For example, we need that to store a list of something: users, goods, HTML elements etc.
66

77
It is not convenient to use an object here, because it provides no methods to manage the order of elements. We can’t insert a new property “between” the existing ones. Objects are just not meant for such use.
88

9-
There exists a special data structure named `Array`, to store ordered collections.
9+
There exists a special data structure named `Array`, to store ordered collections.
1010

1111
## Declaration
1212

@@ -81,10 +81,10 @@ arr[3](); // hello
8181

8282
````smart header="Trailing comma"
8383
An array, just like an object, may end with a comma:
84-
```js
84+
```js
8585
let fruits = [
86-
"Apple",
87-
"Orange",
86+
"Apple",
87+
"Orange",
8888
"Plum"*!*,*/!*
8989
];
9090
```
@@ -106,7 +106,7 @@ Arrays support both operations.
106106

107107
In practice we need it very often. For example, a queue of messages that need to be shown on-screen.
108108

109-
There's another use case for arrays -- the data structure named [stack](https://en.wikipedia.org/wiki/Stack_(abstract_data_type)).
109+
There's another use case for arrays -- the data structure named [stack](https://en.wikipedia.org/wiki/Stack_(abstract_data_type)).
110110

111111
It supports two operations:
112112

@@ -121,7 +121,7 @@ A stack is usually illustrated as a pack of cards: new cards are added to the to
121121

122122
For stacks, the latest pushed item is received first, that's also called LIFO (Last-In-First-Out) principle. For queues, we have FIFO (First-In-First-Out).
123123

124-
Arrays in JavaScript can work both as a queue and as a stack. They allow you to add/remove elements both to/from the beginning or the end.
124+
Arrays in JavaScript can work both as a queue and as a stack. They allow you to add/remove elements both to/from the beginning or the end.
125125

126126
In computer science the data structure that allows it is called [deque](https://en.wikipedia.org/wiki/Double-ended_queue).
127127

@@ -189,11 +189,11 @@ alert( fruits );
189189

190190
## Internals
191191

192-
An array is a special kind of object. The square brackets used to access a property `arr[0]` actually come from the object syntax. Numbers are used as keys.
192+
An array is a special kind of object. The square brackets used to access a property `arr[0]` actually come from the object syntax. That's essentially the same as `obj[key]`, where `arr` is the object, while numbers are used as keys.
193193

194194
They extend objects providing special methods to work with ordered collections of data and also the `length` property. But at the core it's still an object.
195195

196-
Remember, there are only 7 basic types in JavaScript. Array is an object and thus behaves like an object.
196+
Remember, there are only 7 basic types in JavaScript. Array is an object and thus behaves like an object.
197197

198198
For instance, it is copied by reference:
199199

@@ -203,7 +203,7 @@ let fruits = ["Banana"]
203203
let arr = fruits; // copy by reference (two variables reference the same array)
204204

205205
alert( arr === fruits ); // true
206-
206+
207207
arr.push("Pear"); // modify the array by reference
208208

209209
alert( fruits ); // Banana, Pear - 2 items now
@@ -229,7 +229,7 @@ But the engine will see that we're working with the array as with a regular obje
229229

230230
The ways to misuse an array:
231231

232-
- Add a non-numeric property like `arr.test = 5`.
232+
- Add a non-numeric property like `arr.test = 5`.
233233
- Make holes, like: add `arr[0]` and then `arr[1000]` (and nothing between them).
234234
- Fill the array in the reverse order, like `arr[1000]`, `arr[999]` and so on.
235235

@@ -296,7 +296,7 @@ let fruits = ["Apple", "Orange", "Plum"];
296296

297297
// iterates over array elements
298298
for (let fruit of fruits) {
299-
alert( fruit );
299+
alert( fruit );
300300
}
301301
```
302302

@@ -320,7 +320,7 @@ But that's actually a bad idea. There are potential problems with it:
320320

321321
There are so-called "array-like" objects in the browser and in other environments, that *look like arrays*. That is, they have `length` and indexes properties, but they may also have other non-numeric properties and methods, which we usually don't need. The `for..in` loop will list them though. So if we need to work with array-like objects, then these "extra" properties can become a problem.
322322

323-
2. The `for..in` loop is optimized for generic objects, not arrays, and thus is 10-100 times slower. Of course, it's still very fast. The speedup may only matter in bottlenecks or seem irrelevant. But still we should be aware of the difference.
323+
2. The `for..in` loop is optimized for generic objects, not arrays, and thus is 10-100 times slower. Of course, it's still very fast. The speedup may only matter in bottlenecks. But still we should be aware of the difference.
324324

325325
Generally, we shouldn't use `for..in` for arrays.
326326

@@ -338,7 +338,7 @@ fruits[123] = "Apple";
338338
alert( fruits.length ); // 124
339339
```
340340

341-
Note that we usually don't use arrays like that.
341+
Note that we usually don't use arrays like that.
342342

343343
Another interesting thing about the `length` property is that it's writable.
344344

@@ -385,7 +385,7 @@ To evade such surprises, we usually use square brackets, unless we really know w
385385

386386
## Multidimensional arrays
387387

388-
Arrays can have items that are also arrays. We can use it for multidimensional arrays, to store matrices:
388+
Arrays can have items that are also arrays. We can use it for multidimensional arrays, for example to store matrices:
389389

390390
```js run
391391
let matrix = [
@@ -445,7 +445,7 @@ Array is a special kind of object, suited to storing and managing ordered data i
445445

446446
The call to `new Array(number)` creates an array with the given length, but without elements.
447447

448-
- The `length` property is the array length or, to be precise, its last numeric index plus one. It is auto-adjusted by array methods.
448+
- The `length` property is the array length or, to be precise, its last numeric index plus one. It is auto-adjusted by array methods.
449449
- If we shorten `length` manually, the array is truncated.
450450

451451
We can use an array as a deque with the following operations:
@@ -461,4 +461,3 @@ To loop over the elements of the array:
461461
- `for (let i in arr)` -- never use.
462462

463463
We will return to arrays and study more methods to add, remove, extract elements and sort arrays in the chapter <info:array-methods>.
464-

0 commit comments

Comments
 (0)