xCatalog [1]typeof [2]instanceof] [3]constructor[4]tostring front words
JavaScript has a complex type system, and type recognition is the basic function. JavaScript provides a total of four types of recognition methods, this article will be described in detail in these four ways
typeof operator
typeof is a unary operator placed before a single operand, and the return value is a string representing the first letter of the operand type.
[Note that the]typeof operator can be followed with no parentheses
Console.log (typeof ' a '); // ' string 'console.log (typeof (' a ')); // ' String '
Recognition
"1" Identifies the standard type (recognizes null as ' object ')
"2" does not recognize specific object types (except function)
Console.log (typeof"Jerry");//"string"Console.log (typeof12);//"Number"Console.log (typeof true);//"Boolean"Console.log (typeofundefined);//"undefined"Console.log (typeof NULL);//"Object"Console.log (typeof{Name: "Jerry"});//"Object"Console.log (typeof function(){});//"function"Console.log (typeof[]);//"Object"Console.log (typeof NewDate);//"Object"Console.log (typeof/\d/);//"Object"functionPerson () {};console.log (typeof Newperson);//"Object"
[note] The best way to determine whether a value is a null type is to make an identical comparison directly and null
Console.log (typeofnull); // ' object 'console.log (nullnull); // true null); // false null); // false
instanceof operator
Instanceof is a two-dollar operator, the left operand is an object, and the right operand is a constructor. The expression returns true if the left-hand object is an instance object of the right-hand constructor; otherwise false
If the left operand is not an object, returns false and throws a type error exception if the right operand is not a function typeerror
instanceof function (){}); // false // uncaught Typeerror:right-hand side of ' instanceof ' isn't an object instanceof 123);
[note] All objects are instances of object
Recognition
"1" identifies built-in object types, custom types, and their parent types
"2" does not recognize the standard type and will return false
"3" does not recognize undefined, NULL, error
Console.log ("Jerry"instanceofString);//falseConsole.log (12instanceofnumber);//falseConsole.log (true instanceofBoolean);//falseConsole.log (undefinedinstanceofUndefined);//ErrorConsole.log (NULL instanceofNull);//ErrorConsole.log ({name: "Jerry"}instanceofObject);//trueConsole.log (function(){}instanceofFunction);//trueConsole.log ([]instanceofArray);//trueConsole.log (NewDateinstanceofDate);//trueConsole.log (/\d/instanceofREGEXP);//truefunctionPerson () {};console.log (NewPersoninstanceofperson);//trueConsole.log (NewPersoninstanceofObject);//true
Constructor property
The constructor property of the instance object points to its constructor. If it is a built-in type, the output function data type () {[native code]}, or the output function data type () {} if it is a custom type
Recognition
"1" identifies standard type, built-in object type, and custom type
"2" does not recognize undefined, NULL, error, because they do not have a constructor function
Console.log (("Jerry"). Constructor);//function String () {[native code]}Console.log ((+). constructor);//function Number () {[native code]}Console.log ((true). constructor);//function Boolean () {[native code]}Console.log ((undefined). constructor);//ErrorConsole.log ((NULL). constructor);//ErrorConsole.log ({name: "Jerry"}). constructor);//function Object () {[native code]}Console.log ((function() {}). constructor);//function function () {[native code]}Console.log ([[]). constructor);//function Array () {[native code]}Console.log ((NewDate). constructor);//function Date () {[native code]}Console.log ((/\d/). constructor);//function RegExp () {[native code]}functionPerson () {};console.log (Newperson). constructor);//function Person () {}
You can encapsulate the constructor attribute as a type recognition method
function type (obj) { var temp = obj.constructor.toString (); return temp.replace (/^function (\w+) \ (\). +$/, ' $ ');}
functiontype (obj) {vartemp =obj.constructor.toString (). toLowerCase (); returnTemp.replace (/^function (\w+) \ (\). +$/, ' $ ');} Console.log (Type ("Jerry"));//"string"Console.log (Type (12));//"Number"Console.log (Type (true));//"Boolean"Console.log (type (undefined));//ErrorConsole.log (Type (NULL));//ErrorConsole.log (Type ({name: "Jerry"}));//"Object"Console.log (Type (function(){}));//"function"Console.log (Type ([]));//"Array"Console.log (Type (NewDate));//"Date"Console.log (Type (/\d/));//"RegExp"functionPerson () {};console.log (Type (Newperson));//"Person"
Object.prototype.toString () method
The class property of an object is a string that represents the type information of the object. JavaScript does not provide a way to set this property, but there is an indirect way to query it. The Object.prototype.toString () method returns a string in the following format:[Object data type]
Recognition
"1" identifies standard types and built-in object types
' 2 ' does not recognize a custom type
Console.log (Object.prototype.toString.call ("Jerry"));//[Object String]Console.log (Object.prototype.toString.call (12));//[Object number]Console.log (Object.prototype.toString.call (true));//[Object Boolean]Console.log (Object.prototype.toString.call (undefined));//[Object Undefined]Console.log (Object.prototype.toString.call (NULL));//[Object Null]Console.log (Object.prototype.toString.call ({name: "Jerry"}));//[Object Object]Console.log (Object.prototype.toString.call (function(){}));//[Object Function]Console.log (Object.prototype.toString.call ([]));//[Object Array]Console.log (Object.prototype.toString.call (NewDate));//[Object Date]Console.log (Object.prototype.toString.call (/\d/));//[Object RegExp]functionPerson () {};console.log (Object.prototype.toString.call (Newperson));//[Object Object]
The Object.prototype.toString () method can be encapsulated as a type recognition method
functiontype (obj) {returnObject.prototype.toString.call (obj). Slice (8,-1). toLowerCase ();} Console.log (Type ("Jerry"));//"string"Console.log (Type (12));//"Number"Console.log (Type (true));//"Boolean"Console.log (type (undefined));//"undefined"Console.log (Type (NULL));//"NULL"Console.log (Type ({name: "Jerry"}));//"Object"Console.log (Type (function(){}));//"function"Console.log (Type ([]));//"Array"Console.log (Type (NewDate));//"Date"Console.log (Type (/\d/));//"RegExp"functionPerson () {};console.log (Type (Newperson));//"Object"
JavaScript four types of recognition methods