You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: 1-js/04-object-basics/05-object-toprimitive/article.md
+10-14Lines changed: 10 additions & 14 deletions
Original file line number
Diff line number
Diff line change
@@ -3,28 +3,24 @@
3
3
4
4
What happens when objects are added `obj1 + obj2`, subtracted `obj1 - obj2` or printed using `alert(obj)`?
5
5
6
-
There are special methods in objects that do the conversion.
6
+
In that case objects are auto-converted to primitives, and then the operation is carried out.
7
7
8
-
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.
8
+
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.
9
9
10
-
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.
11
-
12
-
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.
13
-
14
-
As for the string conversion -- it usually happens when we output an object like `alert(obj)` and in similar contexts.
10
+
1. All objects are `true` in a boolean context. There are only numeric and string conversions.
11
+
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.
12
+
3. As for the string conversion -- it usually happens when we output an object like `alert(obj)` and in similar contexts.
15
13
16
14
## ToPrimitive
17
15
18
-
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)).
19
-
20
-
That algorithm allows us to customize the conversion using a special object method.
16
+
We can fine-tune string and numeric conversion, using special object methods.
21
17
22
-
Depending on the context, the conversion has a so-called "hint".
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.
23
19
24
20
There are three variants:
25
21
26
22
`"string"`
27
-
: When an operation expects a string, for object-to-string conversions, like `alert`:
23
+
: For an object-to-string conversion, when we're doing an operation on an object that expects a string, like `alert`:
28
24
29
25
```js
30
26
// output
@@ -35,7 +31,7 @@ There are three variants:
35
31
```
36
32
37
33
`"number"`
38
-
: When an operation expects a number, for object-to-number conversions, like maths:
34
+
: For an object-to-number conversion, like when we're doing maths:
39
35
40
36
```js
41
37
// explicit conversion
@@ -52,7 +48,7 @@ There are three variants:
52
48
`"default"`
53
49
: Occurs in rare cases when the operator is "not sure" what type to expect.
54
50
55
-
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.
51
+
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.
0 commit comments