()
The valueOf of an object is intended to return the object's original value and will be automatically executed where the object needs to be converted to the original value. Click here for details.
()
The toString() method returns a string representing the object, and will be automatically executed where the object is expected to be converted into a string. The default toString() method of the object will return [object type], which is the name of the object constructor. Click here for details.
(hint)
The method functions the same as valueOf(), but the priority is higher than valueOf(); and the method will also accept a parameter hint, which is used to represent the specific type of the original value to be converted, and there are several types:
- number: numeric type
- string: string type
- default: default
let obj = { [](hint) { switch (hint) { case 'number': return 123; case 'string': return 'str'; case 'default': return 'default'; default: throw new Error(); } } }; 2 * obj // 246 3 + obj // '3default' obj == 'default' // true String(obj) // 'str'
Object converts original value
All three methods are triggered when the object is expected to be converted to some original value.
1. Expected to be converted to string type
The corresponding hint type is string
Where output is performed, such as alert()
String(obj)
let a = { toString () { return '2' } } (String(a)) // 2
String concatenation (+) operation
let a = { toString () { return '2' } } (a + 'vv')
Template string
let a = { [] (hint) { (hint) // string return 2 } } (`You're old${a}?`) // Are you Old 2?
2. Expected to be converted to numeric types
The corresponding int type is number
division:
let a = { valueOf () { return 2 } } (2 / a, a / 2) // 1 1
Number(obj):
let a = { [] (hint) { (hint) // number return 2 } } (Number(a)) // 2
Positive and negative signs (note that it is not addition and subtraction operations):
let a = { [] (hint) { (hint) // number return 2 } } (+a) // 2 (-a) // -2
3. Expected to be converted to default type (other)
The corresponding hint type is default
Addition of numbers (that is, the one side added to the object is a numeric type):
let a = { [] (hint) { (hint) // default return 2 } } (1 + a) // 3
This is a bit surprising. I thought that the type converted in such a situation should be a numeric type, but in fact it is default;
Boolean operation: All objects are converted to true;
let a = { [] (hint) { (hint) // No trigger return false } } (Boolean(a), a && 123) // true 123
Boolean operations include ==
The order of triggering of three methods
First, determine whether the object has a (hint) method. If there is, execute the method. If there is no, execute the following steps;
If it is expected to be converted to a string type, the toString() method is preferred;
If it is expected to be converted to the default type or numeric type, the valueOf() method is preferred:
Note: If there is no valueOf() method, but the toString() method is defined, the toString() method will be executed;
Summarize
That’s all for this article. I hope it can help you and I hope you can pay more attention to more of my content!