Skip to content

Commit a956c4f

Browse files
committed
up
1 parent e4288e4 commit a956c4f

File tree

1 file changed

+10
-14
lines changed
  • 1-js/04-object-basics/05-object-toprimitive

1 file changed

+10
-14
lines changed

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

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,24 @@
33

44
What happens when objects are added `obj1 + obj2`, subtracted `obj1 - obj2` or printed using `alert(obj)`?
55

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.
77

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.
99

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.
1513

1614
## ToPrimitive
1715

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.
2117

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.
2319

2420
There are three variants:
2521

2622
`"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`:
2824

2925
```js
3026
// output
@@ -35,7 +31,7 @@ There are three variants:
3531
```
3632

3733
`"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:
3935

4036
```js
4137
// explicit conversion
@@ -52,7 +48,7 @@ There are three variants:
5248
`"default"`
5349
: Occurs in rare cases when the operator is "not sure" what type to expect.
5450

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.
5652

5753
```js
5854
// binary plus

0 commit comments

Comments
 (0)