Catalog [1]typeof [2]tostring [3]constructor] [4]instanceoftypeof
The typeof operator is used to return a type that is using a value
"Output" lowercase string as the first letter
function
[1] The standard type can be recognized (recognizes null as Object)
[2] No specific object type is recognized (except function)
Instance
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"
Object.prototype.toString
Returns a string representation representing the object
String form of "Output" [Object data Type]
function
[1] standard types and built-in object types can be identified
[2] Custom type not recognized
"Construction Method"
function type (obj) { return Object.prototype.toString.call (obj). Slice (8,-1). toLowerCase ();
"Instance 1"
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]
"Instance 2"
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"
Constructor
The constructor instance has a constructor property that points to its constructor
"Output" function data type () {[native code]} or function custom type () {}
function
[1] can recognize standard type, built-in object type and custom type
[2] cannot recognize undefined, NULL, will error
"Construction Method"
function type (obj) { var temp = obj.constructor.toString (); return temp.replace (/^function (\w+) \ (\). +$/, ' $ ');}
"Instance 1"
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);//Error
Console.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 () {}
"Instance 2"
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));//Error
console.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"
instanceof
The instanceof operator can determine whether an object is an instance of a particular constructor
"Output" true or False
function
[1] can identify built-in object type, custom type and its parent type
[2] cannot recognize standard type, will return false
[3] cannot recognize undefined, NULL, will error
Instance
Console.log ("Jerry"instanceofString);//falseConsole.log (12instanceofnumber);//falseConsole.log (true instanceofBoolean);//false
console.log (undefined instanceof undefined);//Error
console.log (null instanceof NULL);//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
4 types of recognition methods in JavaScript