Implicit JavaScript Conversion
JavaScript data types include null, undefined, boolean, string, number, and object. Object is a reference type. The other five types are basic or original types. We can use the typeof method to print a type. To compare variables of different types, you must first convert them to type, which is called type conversion and type conversion is also called implicit conversion. Implicit conversion usually occurs in the addition, subtraction, multiplication, division, equals, and smaller than, greater than, and so on .. Typeof '11' // string typeof (11) // number '11' <4 // the conversion of the basic type of "false" is described as follows: 1. when a string is added with a number, the number is converted into a string. 2. subtract a string from a number and convert it into a number. If the string is not a pure number, it is converted to NaN. The same is true for string subtraction. The two strings are reduced to numbers first. 3. multiplication, division, greater than, less than and minus conversion is the same. // Implicit conversion +-* ==/// + 10 + '20' // 2010 //-10-'20' //-1010-'one' // NaN10 -'100a' // NaN // * 10 * '20' // 200 '10' * '20' // 200 // 20/'10' // 2' 20'/'10' // 2 '20'/'one' // NaN to check a group =. 1. undefined is equal to null 2. when comparing strings and numbers, the string is converted to numbers 3. when the number is a Boolean comparison, Boolean to number 4. when comparing string and Boolean, convert the two to numbers // = undefined = null; // true '0' = 0; // true, and convert string to numbers 0 = false; // true, Boolean to numeric '0' = false; // true, convert both to numeric null = false; // falseundefined = false; // It is relatively simple to convert the basic types of the false reference type. The comparison between the reference type and the basic type is relatively complicated. First, convert the reference type to the basic type and then compare it according to the above method. All the values of the reference type to boolean are true. For example, if an empty array is of the reference type as long as it is an object, [] is true. To convert a reference type to a number or string, valueOf () or toString () is required. The object itself inherits valuOf () and toString (), and you can also customize valueOf () and toString (). The inherited valueOf () is converted into a string, number, or itself based on different objects, and the object must be converted into a string using toString. Generally, valueOf () is called for an object by default (). 1. valueOf (); 2. when the object is converted to a string, call toString (). First, let's take a look at the following example: 0 = []; // true, 0 = []. valueOf ();-> 0 = 0; '0' = []; // false, '0' = []. toString ();-> '0' = ''; 2 = ['2']; // true, 2 = ['2']. valueOf ();-> 2 = '2'-> 2 = 2; '2' = [2]; // true, '2' = [2]. toString ();-> '2' = '2'; [] =! []; // True, []. valueOf () =! Boolean ([])-> 0 = false-> 0 = 0; valueOf () is called when the object is converted to a number. toString () is called before this (); so I guess the valueOf method is like this. So the above example 0 = [] should be changed to the following more reasonable. In any case, [] is finally converted to 0. Var valueOf = function () {var str = this. toString (); // call toString () first and convert it to a string //...} 0 = []; // true, 0 = []. valueOf ();-> 0 = '0'-> 0 = 0; custom valueOf () and toString (); 1. the custom valueOf () and toString () exist. valueOf () is called by default. 2. if you only have toString (), call toString (); var a = [1];. valueOf = function () {return 1;}. toString = function () {return '1';} a + 1; // 2, valueOf () First Call remove valueOf () and then call toString (). Var a = [1];. valueOf = function () {return 1;}. toString = function () {return '1';} a + 1; // 2, first call valueOf () // remove valueOfdelete. valueOf; a + 1; // '11', what if toString () is called and other operations are returned? Var a = [1];. valueOf = function () {return;}. toString = function () {return 1 ;}; 1-a; // other NaN objects call valueOf () to convert to different types: var a ={};. valueOf (); // Object {} var a = [];. valueOf (); // [] var a = new Date ();. valueOf (); // 1423812036234 numeric var a = new RegExp ();. valueOf ();///(? :)/The comparison between the reference types of the regular object is the memory address comparison, and implicit conversion is not required. [] = [] // False address is different var a = []; B = a // true Explicit conversions are relatively simple, classes can be directly converted as methods. Number ([]); // 0 String ([]); // ''boolean ([]); // true provides a simpler conversion method. 3 + ''// string '3' + '3' // number 3 !! '3' // true