up
This commit is contained in:
parent
e4288e4ec3
commit
a956c4f0a6
1 changed files with 10 additions and 14 deletions
|
@ -3,28 +3,24 @@
|
||||||
|
|
||||||
What happens when objects are added `obj1 + obj2`, subtracted `obj1 - obj2` or printed using `alert(obj)`?
|
What happens when objects are added `obj1 + obj2`, subtracted `obj1 - obj2` or printed using `alert(obj)`?
|
||||||
|
|
||||||
There are special methods in objects that do the conversion.
|
In that case objects are auto-converted to primitives, and then the operation is carried out.
|
||||||
|
|
||||||
In the chapter <info:type-conversions> we've seen the rules for numeric, string and boolean conversions of primitives. But we left a gap for objects. Now, as we know about methods and symbols it becomes possible to close it.
|
In the chapter <info:type-conversions> we've seen the rules for numeric, string and boolean conversions of primitives. But we left a gap for objects. Now, as we know about methods and symbols it becomes possible to fill it.
|
||||||
|
|
||||||
For objects, there's no to-boolean conversion, because all objects are `true` in a boolean context. So there are only string and numeric conversions.
|
1. All objects are `true` in a boolean context. There are only numeric and string conversions.
|
||||||
|
2. The numeric conversion happens when we subtract objects or apply mathematical functions. For instance, `Date` objects (to be covered in the chapter <info:date>) can be subtracted, and the result of `date1 - date2` is the time difference between two dates.
|
||||||
The numeric conversion happens when we subtract objects or apply mathematical functions. For instance, `Date` objects (to be covered in the chapter <info:date>) can be subtracted, and the result of `date1 - date2` is the time difference between two dates.
|
3. As for the string conversion -- it usually happens when we output an object like `alert(obj)` and in similar contexts.
|
||||||
|
|
||||||
As for the string conversion -- it usually happens when we output an object like `alert(obj)` and in similar contexts.
|
|
||||||
|
|
||||||
## ToPrimitive
|
## ToPrimitive
|
||||||
|
|
||||||
When an object is used in the context where a primitive is required, for instance, in an `alert` or mathematical operations, it's converted to a primitive value using the `ToPrimitive` algorithm ([specification](https://tc39.github.io/ecma262/#sec-toprimitive)).
|
We can fine-tune string and numeric conversion, using special object methods.
|
||||||
|
|
||||||
That algorithm allows us to customize the conversion using a special object method.
|
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.
|
||||||
|
|
||||||
Depending on the context, the conversion has a so-called "hint".
|
|
||||||
|
|
||||||
There are three variants:
|
There are three variants:
|
||||||
|
|
||||||
`"string"`
|
`"string"`
|
||||||
: When an operation expects a string, for object-to-string conversions, like `alert`:
|
: For an object-to-string conversion, when we're doing an operation on an object that expects a string, like `alert`:
|
||||||
|
|
||||||
```js
|
```js
|
||||||
// output
|
// output
|
||||||
|
@ -35,7 +31,7 @@ There are three variants:
|
||||||
```
|
```
|
||||||
|
|
||||||
`"number"`
|
`"number"`
|
||||||
: When an operation expects a number, for object-to-number conversions, like maths:
|
: For an object-to-number conversion, like when we're doing maths:
|
||||||
|
|
||||||
```js
|
```js
|
||||||
// explicit conversion
|
// explicit conversion
|
||||||
|
@ -52,7 +48,7 @@ There are three variants:
|
||||||
`"default"`
|
`"default"`
|
||||||
: Occurs in rare cases when the operator is "not sure" what type to expect.
|
: Occurs in rare cases when the operator is "not sure" what type to expect.
|
||||||
|
|
||||||
For instance, binary plus `+` can work both with strings (concatenates them) and numbers (adds them), so both strings and numbers would do. Or when an object is compared using `==` with a string, number or a symbol.
|
For instance, binary plus `+` can work both with strings (concatenates them) and numbers (adds them), so both strings and numbers would do. Or when an object is compared using `==` with a string, number or a symbol, it's also unclear which conversion should be done.
|
||||||
|
|
||||||
```js
|
```js
|
||||||
// binary plus
|
// binary plus
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue