Determine the type in js: typeof/instanceof/constructor/prototype
How can we determine the type in js? Here are several examples:
Var a = "jason ";
Var B = 123;
Var c = true;
Var d = [1, 2, 3];
Var e = new Date ();
Var f = function (){
Alert ('jason ');
};
I. The most common judgment method: typeof
Typeof is a unary operator. It returns a string and returns different results for different operands. In addition, typeof can determine the function type; it is easier to determine objects except Object type.
Console. log (typeof a = "string"); // true
Console. log (typeof a = String); // false
The specific rules are as follows:
1) For numeric operands, the value returned by typeof is number. For example, for typeof 1, the returned value is number.
The preceding figure shows the regular number. For an unconventional number type, the result returned is also number. For example, typeof NaN, NaN represents a special non-numeric value in JavaScript, although it is a numerical type.
In JavaScript, there are several special numeric types:
Infinity // special value of Infinity
NaN // special non-numeric value
Number. MAX_VALUE // The maximum Number that can be expressed.
Number. MIN_VALUE // the smallest Number that can be expressed (closest to zero)
Number. NaN // special non-numeric value
Number. POSITIVE_INFINITY // special value indicating positive infinity
Number. NEGATIVE_INFINITY // special value indicating negative infinity
For the above special types, when typeof is used for calculation, the result will be number.
2) For the string type, the value returned by typeof is string. For example, the value returned by typeof "jason" is string.
3) for the boolean type, the value returned by typeof is boolean. For example, if typeof true, the returned value is boolean.
4) for objects, arrays, and null, the returned values are objects. For example, typeof {}, typeof [], and typeof null all return object values.
5) for the function type, the returned value is function. For example, typeof eval and typeof Date all return function values.
6) if the number of operations is not defined (such as non-existent variables, functions, or undefined), undefined is returned. For example, typeof jason and typeof undefined both return undefined.
Console. log (typeof a); // string
Console. log (typeof B); // number
Console. log (typeof c); // boolean
Console. log (typeof d); // object
Console. log (typeof e); // object
Console. log (typeof f); // function
Console. log (typeof 1); // number
Console. log (typeof NaN); // number
Console. log (typeof Number. MIN_VALUE); // number
Console. log (typeof Infinity); // number
Console. log (typeof "123"); // string
Console. log (typeof true); // boolean
Console. log (typeof {}); // object
Console. log (typeof []); // object
Console. log (typeof null); // object
Console. log (typeof eval); // function
Console. log (typeof Date); // function
Console. log (typeof sss); // undefined
Console. log (typeof undefined); // undefined
2. determine whether an object is of a certain data type or whether a variable is an instance of an object: instanceof
Note: instanceof must be followed by the object type, and the case cannot be incorrect. This method is suitable for selecting conditions or branches.
Console. log (d instanceof Array); // true
Console. log (e instanceof Date); // true
Console. log (f instanceof Function); // true
3. constructor: constructor
Console. log (d. constructor === Array) // true
Console. log (e. constructor === Date) // true
Console. log (f. constructor === Function) // true
Note that constructor will encounter errors during class inheritance
For example:
Function (){};
Function B (){};
Var aObj = new ();
Console. log (aObj. constructor === A); // true;
Console. log (aObj. constructor === B); // false;
Function C (){};
Function D (){};
C. prototype = new D (); // C inherits from D
Var cObj = new C ();
Console. log (cObj. constructor === C); // false;
Console. log (cObj. constructor === D); // true;
This problem does not occur in the instanceof method, and true is reported for objects directly inherited or indirectly inherited:
Console. log (cObj instanceof C); // true
Console. log (cObj instanceof D); // true
To solve the construtor problem, we usually let the constructor of the object point to itself manually:
CObj. constructor = C; // assign your class to the constructor attribute of the object.
Console. log (cObj. constructor === C); // true;
Console. log (cObj. constructor = D); // false; the base class will not report true;
Iv. Common but tedious method: prototype
Console. log (Object. prototype. toString. call (a) === '[object String]'); // true
Console. log (Object. prototype. toString. call (B) === '[ object Number] '); // true
Console. log (Object. prototype. toString. call (c) ===' [object Boolean] '); // true
Console. log (Object. prototype. toString. call (d) === '[object Array]'); // true
Console. log (Object. prototype. toString. call (e) ===' [object Date] '); // true
Console. log (Object. prototype. toString. call (f) === '[object Function]'); // true
Note: The case cannot be entered incorrectly, Which is troublesome, but better than common.
Summary:
Generally, you can use typeof to determine the Object type. You can use the instanceof or constructor Method to Predict the Object type. To sum up, please add it!