Which objects have valueOf() and toString() methods?
- To be clear, all types except null and undefined have or inherit valueOf() and toString() methods;
- Object.prototype.valueof () returns the original valueOf the specified Object;
- Object. The prototype. The toString () returns a string said the Object.
- These two methods are often used in the Primitive abstraction operation; By default, this abstract operation executes the valueOf method first, continues toString if it does not return the original value, and raises an error if it does not return the original value. If the conversion type is specified, this will not be the case;
- Array,Function,Math, and Error objects do not have valueOf methods themselves. They inherit the original object.prototype.valueof () method and return the type itself. Such as:
[].valueOf()/ / []
function A(){};
A.valueOf();//function A(){}
Copy the code
(Note that some of the valueOf and toString methods are overridden on Date, array, etc., so calling this method may result in different operations for different objects. If not, the original valueOf and toString methods will be called.)
Implicit conversion
Implicit conversions occur with some operators, such as meta operators, bitwise operators, additive operators, multiplicative operators, equality operators, etc. There are explicit conversion rules for these operators in the Little Red Book;
- Equality operator:
- If any of the operands are Boolean values, they are converted to values and then compared.
- If one operand is a string and the other is a value, it’s more valuable to convert the string to a value first;
- If one operand is an object and the other is not, the valueOf method of the object is called and the resulting primitive types are compared according to the previous rules.
ValueOf and toString Priorities
Object to string
In general, the object-to-string conversion goes through the following steps: 1. If the object has the toString() method, it is called. If it returns a raw value, JS converts the value to a string and returns the string result. 2. If the object does not have a toString() method, or the method does not return a raw value, then the valueOf() method is called. 3. Otherwise, JS cannot get a raw value from toString() or valueOf(), so it will throw a type error exception.
Object to number
In general, js does the same thing when converting objects to numbers, but here it first tries to use the valueOf() method: 1. If the object has a valueOf() method that returns a raw value, then JS converts the raw value to a number and returns the number. 2. Otherwise, if the object has a toString() method, which returns a raw value, js will convert and return. Js converts the original value to the corresponding string value, and then converts the original value to the corresponding numeric type. Otherwise, JS throws a type error exception.
JS core built-in class
An object is converted to its original value by a toString or valueOf method. The built-in classes at the core of the JS language call valueOf() first and then try toString(). For all non-date objects, the conversion from an object to a raw value is basically an object to a number
ValueOf and toString overrides
If the object overrides only toString, the object will be converted regardless of valueOf. However, if only the valueOf method is overridden, the valueOf method takes precedence when converting to a string.
Taking a
- []==! []
- Solution: According to the first equality operator, first! [], convert to 0; The equality operator then calls [] to valueOf, but not the original value, and toString, which returns the empty string. The equality operator then converts the empty string to a valueOf 0. Therefore, the final result is true.