commit
1cf212893a
3 changed files with 4 additions and 4 deletions
|
@ -175,7 +175,7 @@ Then their references are marked:
|
||||||
|
|
||||||

|

|
||||||
|
|
||||||
...And their refences, while possible:
|
...And their references, while possible:
|
||||||
|
|
||||||

|

|
||||||
|
|
||||||
|
|
|
@ -421,7 +421,7 @@ alert( [1] + 1 ); // "11"
|
||||||
alert( [1,2] + 1 ); // "1,21"
|
alert( [1,2] + 1 ); // "1,21"
|
||||||
```
|
```
|
||||||
|
|
||||||
Arrays do not have `Symbol.toPrimitive`, neither a viable `valueOf`, they implement only `toString` conversion, so here `[]` becomes an empty string, `[1]` becomes `"1"` and `[1,2]` becomes `"1,2".
|
Arrays do not have `Symbol.toPrimitive`, neither a viable `valueOf`, they implement only `toString` conversion, so here `[]` becomes an empty string, `[1]` becomes `"1"` and `[1,2]` becomes `"1,2"`.
|
||||||
|
|
||||||
When the binary plus `"+"` operator adds something to a string, it converts it to a string as well, so the next step looks like this:
|
When the binary plus `"+"` operator adds something to a string, it converts it to a string as well, so the next step looks like this:
|
||||||
|
|
||||||
|
|
|
@ -100,7 +100,7 @@ alert( arr ); // "I", "study", "complex", "language", "JavaScript"
|
||||||
```
|
```
|
||||||
|
|
||||||
````smart header="Negative indexes allowed"
|
````smart header="Negative indexes allowed"
|
||||||
Here and in other array methods, negative indexes are allowed. The specify the position from the end of the array, like here:
|
Here and in other array methods, negative indexes are allowed. They specify the position from the end of the array, like here:
|
||||||
|
|
||||||
```js run
|
```js run
|
||||||
let arr = [1, 2, 5]
|
let arr = [1, 2, 5]
|
||||||
|
@ -382,7 +382,7 @@ alert(arr); // *!*1, 2, 15*/!*
|
||||||
|
|
||||||
Now it works as intended.
|
Now it works as intended.
|
||||||
|
|
||||||
Let's step aside and thing what's happening. The `arr` can be array of anything, right? It may contain numbers or strings or html elements or whatever. We have a set of *something*. To sort it, we need an *ordering function* that knows how to compare its elements. The default is a string order.
|
Let's step aside and think what's happening. The `arr` can be array of anything, right? It may contain numbers or strings or html elements or whatever. We have a set of *something*. To sort it, we need an *ordering function* that knows how to compare its elements. The default is a string order.
|
||||||
|
|
||||||
The `arr.sort(fn)` method has a built-in implementation of sorting algorithm. We don't need to care how it exactly works (an optimized [quicksort](https://en.wikipedia.org/wiki/Quicksort) most of the time). It will walk the array, compare its elements using the provided function and reorder them, all we need is to provide the `fn` which does the comparison.
|
The `arr.sort(fn)` method has a built-in implementation of sorting algorithm. We don't need to care how it exactly works (an optimized [quicksort](https://en.wikipedia.org/wiki/Quicksort) most of the time). It will walk the array, compare its elements using the provided function and reorder them, all we need is to provide the `fn` which does the comparison.
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue