When writing comprespt code, the typeof and instanceof operators are used from time to time and are mandatory. But! Using them is always hard to get the desired results directly, and it is very tangled. It is widely said that "these two operators may be the biggest design defect in javascript, because it is almost impossible to get the desired results from them"
Typeof
Description: typeof returns a string of the expression data type. The returned result is a basic js data type, including number, boolean, string, object, undefined, and function.
From the description, it seems that there is no problem.
The following code writes a numeric variable. The result of typeof is "number ".Copy codeCode: var a = 1;
Console. log (typeof (a); // => number
If the Number Type constructor is used to create a new variable, the result after typeof is "object ".Copy codeThe Code is as follows: var a = new Number (1 );
Console. log (typeof (a); // => object
The above two output results seem to have no problem, which seems to be a matter of course in the book, because javascript is designed in this way.
But! The problem is that if typeof is called, the type of a variable should be returned accurately, whether directly created with a value or created with a Type constructor, otherwise! What else do I do with you!
For:Copy codeCode: var a = 1;
Var B = new Number (1 );
The type of variables a and B should be exactly the Number that is the expected result.
The accurate type information is stored in the value of the variable's internal attribute [[Class], which is obtained by using the toString defined on Object. prototype.
Obtain type information:Copy codeCode: var a = 1;
Var B = new Number (1 );
Console. log (Object. prototype. toString. call ());
Console. log (Object. prototype. toString. call (B ));
Output:Copy codeThe Code is as follows: [object Number]
[Object Number]
Is it already very straightforward? Let's take a look and get the direct result:Copy codeCode: var a = 1;
Var B = new Number (1 );
Console. log (Object. prototype. toString. call (a). slice (8,-1 ));
Console. log (Object. prototype. toString. call (B). slice (8,-1 ));
Output:
Number
Number
This is the expected result.
For better use, we encapsulate a method to determine whether a variable is of a certain type:Copy codeThe Code is as follows: function is (obj, type ){
Var clas = Object. prototype. toString. call (obj). slice (8,-1 );
Return obj! = Undefined & obj! = Null & clas = type;
}
Some variables have been tested. Let's take a look at their typeof output:Copy codeThe Code is as follows: var a1 = 1;
Var a2 = Number (1 );
Var b1 = "hello ";
Var b2 = new String ("hello ");
Var c1 = [1, 2, 3];
Var c2 = new Array (1, 2, 3 );
Console. log ("a1's typeof:" + typeof (a1 ));
Console. log ("a2's typeof:" + typeof (a2 ));
Console. log ("b1's typeof:" + typeof (b1 ));
Console. log ("b2's typeof:" + typeof (b2 ));
Console. log ("c1's typeof:" + typeof (c1 ));
Console. log ("c2's typeof:" + typeof (c2 ));
Output:
A1's typeof: number
A2's typeof: object
B1's typeof: string
B2's typeof: object
C1's typeof: object
C2's typeof: object
Let's use the new functions as follows:Copy codeThe Code is as follows: console. log ("a1 is Number:" + is (a1, "Number "));
Console. log ("a2 is Number:" + is (a2, "Number "));
Console. log ("b1 is String:" + is (b1, "String "));
Console. log ("b2 is String:" + is (b2, "String "));
Console. log ("c1 is Array:" + is (c1, "Array "));
Console. log ("c2 is Array:" + is (c2, "Array "));
Output:
A1 is Number: true
A2 is Number: true
B1 is String: true
B2 is String: true
C1 is Array: true
C2 is Array: true
Note: typeof is not useless. It is used to detect whether a variable has been defined or assigned a value.
Instanceof
Determines whether an object is of a certain data type or whether a variable is an instance of an object.
When the instanceof operator is used to compare two built-in types of variables, it is equally inadequate and will be dissatisfied with the results.Copy codeThe Code is as follows: console. log ("abc" instanceof String); // false
Console. log ("abc" instanceof Object); // false
Console. log (new String ("abc") instanceof String); // true
Console. log (new String ("abc") instanceof Object); // true
The relationship is accurately reflected only when custom objects are compared.Copy codeThe Code is as follows: function Person (){}
Function Man (){}
Man. prototype = new Person ();
Console. log (new Man () instanceof Man); // true
Console. log (new Man () instanceof Person); // true