During the JavaScript program writing process, JS will automatically convert the object into number or string and then process it according to different contexts. The rules for this automatic conversion are as follows:
The rule for automatically converting object to string:
1. If the object belongs to the class overrides the toString() method, the method is called. If the return result of the toString() call is Primitive(string, number, boolean, undefined, null), the Primitive value is converted into string and returned.
2. If the toString() method is not overridden by the class toString(), the return result of the toString() call is "[object Object]"; or the toString() method is overridden but the method returns the result as an object. Then JS will call the object's valueOf() method. If the return result of the valueOf() call is Primitive(string, number, boolean, undefined, null), then convert the Primitive value into string and return.
3. If neither of the above points is met and the Primitive value cannot be obtained by calling the object's toString() method or valueOf() method, then JS will throw a TypeError error.
The rule for automatically converting object to number:
1. Call the valueOf() method of object. If the Primitive value is obtained, convert the Primitive value into number and return it.
2. If the Primitive value cannot be obtained from the valueOf() method, then call the object's toString() method; if toString() returns a Primitive value, then convert the Primitive value to number and return.
3. If neither of the above two points cannot be met, JS will throw a TypeError error.
It can be seen that the rules for automatically converting object to string and object to number are actually the same. The difference is the order of call toString() method and valueOf() method.
According to the above rules, some conversion results can be well understood:
1. For empty arrays, when converting them to number, the result is 0. This is because the valueOf() method of array will be called first. Since valueOf() returns the array object itself, JS will next call the toString() method of the empty array; because the result of the empty array toString() returns to an empty string, the empty string will eventually be converted to a number 0 and returned.
2. For an array with only one number member, apply the same rule to convert it into a number, and the final result is the number.
3. For arrays with multiple numeric members, since the string cannot be converted to number, the final result is NaN.
When to convert to string? When to convert to number?
When performing automatic type conversion on an object, JS will choose to convert it to string or number depending on the object type and operator. The specific rules are as follows:
1. If an object appears on both sides of the + operator, convert the object into a string.
2. All objects (except Date objects) will be converted to number first.
3. For Date objects, priority is converted to string.
It is worth noting that for the + operator, except for the case where objects or strings appear on both sides of the operator, the operation of "convert to number" is performed in other cases. At the same time, this operation is also related to the order between values.
experiment
The code copy is as follows:
console.log(3 * []);//0
console.log(3 * [2]);//6
console.log(3 * [1,2]);//NaN
console.log(3 + [2]);//32
var now = new Date();
console.log(now + 1);//Wed Mar 26 2014 10:51:39 GMT+0800 (CST)1
console.log(now - 1);//1395802299223
console.log(now * 2);//2791604598448
console.log(true + true);//2
console.log(2 + null);//2, null is converted to 0
console.log(2 + undefined);//NaN, undefined is converted to NaN
console.log(1 + 2 + "cats");//3 cats
console.log(1 + (2 + "cats"));//12 cats