When writing Javascirpt code, the two operators, typeof and Instanceof, are used every now and then, which is necessary. But it's not always possible to use them directly to get the results you want, and it's a common myth that "these two operators are probably the biggest design flaws in JavaScript because it's almost impossible to get the results you want from them."
typeof
Description: TypeOf returns a string of data types for an expression, returning the result to JS basic data type, including Number,boolean,string,object,undefined,function.
From the point of view, there seems to be no problem.
The following code writes a numeric variable, the result of which is "number" after typeof.
The code is as follows:
var a = 1;
Console.log (typeof (a)); =>number
If you use the constructor of the number type to new a variable, the result of the TypeOf is "object".
Copy code code as follows:
var a = new number (1);
Console.log (typeof (a)); =>object
The above two outputs seem to have no problem, which seems to be a matter of course in the book, because that's how JavaScript is designed.
But the problem is that if you call typeof, you should exactly return the type of the variable, whether it was created directly with a value or a constructor of a type, otherwise! What am I going to do with you?
So for:
The code is as follows:
var a = 1;
var B = new number (1);
The type of a and B variables is exactly what the number is supposed to be.
The exact type information is stored in the value of the internal property [[Class]] of the variable and is obtained by using the method ToString defined on the Object.prototype.
Get type information:
The code is as follows:
var a = 1;
var B = new number (1);
Console.log (Object.prototype.toString.call (a));
Console.log (Object.prototype.toString.call (b));
Output:
The code is as follows:
[Object number]
[Object number]
Is it already very straightforward, we deal with it a little bit, get direct result:
The code is as follows:
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 result of wanting.
For better use, we encapsulate a method that is used to determine whether a variable is of a certain type:
The 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;
}
To define some variables to test, first take a look at their typeof output:
The 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
The function of our new work is:
The 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 also not useless, the practical use is to detect whether a variable has been defined or has been assigned a value.
instanceof
Description: Determines whether an object is a data type, or whether a variable is an instance of an object.
The instanceof operator can be used to compare two variables with built-in types, as well as to be dissatisfied with the results.
The 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 comparing a custom object.
The code is as follows:
function person () {}
Function Man () {}
Man.prototype = new Person ();
Console.log (New Man () Instanceof Mans); True
Console.log (New Man () instanceof); True