up
This commit is contained in:
parent
a956c4f0a6
commit
2c4cc643af
2 changed files with 27 additions and 26 deletions
|
@ -155,14 +155,21 @@ alert(user + 500); // toString -> John500
|
|||
|
||||
In the absence of `Symbol.toPrimitive` and `valueOf`, `toString` will handle all primitive conversions.
|
||||
|
||||
|
||||
## ToPrimitive and ToString/ToNumber
|
||||
## Return types
|
||||
|
||||
The important thing to know about all primitive-conversion methods is that they do not necessarily return the "hinted" primitive.
|
||||
|
||||
There is no control whether `toString()` returns exactly a string, or whether `Symbol.toPrimitive` method returns a number for a hint "number".
|
||||
|
||||
**The only mandatory thing: these methods must return a primitive.**
|
||||
The only mandatory thing: these methods must return a primitive, not an object.
|
||||
|
||||
```smart header="Historical notes"
|
||||
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.
|
||||
|
||||
In contrast, `Symbol.toPrimitive` *must* return a primitive, otherwise there will be an error.
|
||||
```
|
||||
|
||||
## Further operations
|
||||
|
||||
An operation that initiated the conversion gets that primitive, and then continues to work with it, applying further conversions if necessary.
|
||||
|
||||
|
@ -204,11 +211,6 @@ For instance:
|
|||
alert(obj + 2); // 3 (ToPrimitive returned boolean, not string => ToNumber)
|
||||
```
|
||||
|
||||
```smart header="Historical notes"
|
||||
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).
|
||||
|
||||
In contrast, `Symbol.toPrimitive` *must* return a primitive, otherwise, there will be an error.
|
||||
```
|
||||
|
||||
## Summary
|
||||
|
||||
|
|
|
@ -189,7 +189,7 @@ alert( fruits );
|
|||
|
||||
## Internals
|
||||
|
||||
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.
|
||||
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.
|
||||
|
||||
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.
|
||||
|
||||
|
@ -320,7 +320,7 @@ But that's actually a bad idea. There are potential problems with it:
|
|||
|
||||
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.
|
||||
|
||||
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.
|
||||
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.
|
||||
|
||||
Generally, we shouldn't use `for..in` for arrays.
|
||||
|
||||
|
@ -385,7 +385,7 @@ To evade such surprises, we usually use square brackets, unless we really know w
|
|||
|
||||
## Multidimensional arrays
|
||||
|
||||
Arrays can have items that are also arrays. We can use it for multidimensional arrays, to store matrices:
|
||||
Arrays can have items that are also arrays. We can use it for multidimensional arrays, for example to store matrices:
|
||||
|
||||
```js run
|
||||
let matrix = [
|
||||
|
@ -461,4 +461,3 @@ To loop over the elements of the array:
|
|||
- `for (let i in arr)` -- never use.
|
||||
|
||||
We will return to arrays and study more methods to add, remove, extract elements and sort arrays in the chapter <info:array-methods>.
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue