The first method typeof
TypeOf is an operator that has the following values
<!DOCTYPE HTML><HTMLLang= "en"><Head> <MetaCharSet= "UTF-8"> <title>Ces</title></Head><Body> <Script>Console.log (typeof "Liesbeth");//"string"Console.log (typeof A);//"Number"Console.log (typeof true); //"Boolean"Console.log (typeofundefined);//"undefined"Console.log (typeof NULL);//"Object"Console.log (typeof{name:"Liesbeth"});//"Object"Console.log (typeof function(){});//"function"Console.log (typeof []);//"Object"Console.log (typeof NewDate);//"Object"Console.log (typeof /[0-9]/);//' object 'Console.log (typeof NewNumber (123));//' object ' functionPerson () {}; Console.log (typeof NewPerson );//' object ' </Script></Body></HTML>
typeof
The result of the operation is as follows, it can be seen that typeof can judge the value of the base type, other types created by new except function, the other is object, where null is also an object
The second method instanceof
instanceof
Operator can be used to determine the function of a constructorprototype属性所指向的對象是否存在于另外一个要检测对象的原型链上即某个对象是否是某个构造函数的实例,如下
<!DOCTYPE HTML><HTMLLang= "en"><Head> <MetaCharSet= "UTF-8"> <title>Ces</title></Head><Body> <Script> //ability to discriminate built-in object typeConsole.log ([]instanceofArray);//trueConsole.log ([]instanceofObject);//trueConsole.log (/[0-9]/ instanceofRegExp);//trueConsole.log (NewString ('123') instanceofString);//trueConsole.log (function(){} instanceofFunction);//true //cannot determine the original typeConsole.log ('123' instanceofString);//falseConsole.log (123 instanceofNumber );//falseConsole.log (true instanceofBoolean);//false //distinguishing custom Object types functionPoint (x, y) { This. x=x; This. Y=y; } functionCircle (x,y,r) {Point.call ( This, x, y); This. Radius=R; } Circle.prototype=NewPoint (); Circle.prototype.constructor=Circle; varC= NewCircle (1,1,2); Console.log (cinstanceofCircle);//trueConsole.log (cinstanceofPoint );//true </Script></Body></HTML>
instanceof
Running results such as
It can be seen that instanceof does not recognize the original type, can identify the custom object and the built-in object type and they all belong to object
The third method Object.prototype.toString.call
This method gets the value of the [[Class]] property of the object, returns the result as a fixed ' [object ' +[[class]] property + '] ', and $.type () in jquery is using this property to determine the object type.
function type (obj) { return Object.prototype.toString.call (obj). Slice (8,-1);}
The results of the operation are as follows:
The object that is created when type (c) is instanceof.
You can see that ToString recognizes both the original type and the built-in object, but does not recognize the custom object type, and the null and undefined in IE7 and 8 return "Object" as follows
The string also returns "Object" in IE6, so pay more attention to these values when you use them.
The fourth method of constructor
The constructor property returns a reference to the array function that created this object.
With this feature we can discriminate between types, where null and undefined do not have constructors, so special handling, writing functions as follows
function getconstructorname (obj) { return (obj===undefined| | obj===null)? obj: (obj.constructor&&obj.constructor.tostring (). Match (/function\s* ([^ (]*)/) [1 ]);}
The operation results are as follows
You can see that constructor can discriminate between primitive types (except null and undefined), built-in objects, and custom objects.
Reference: http://www.cnblogs.com/ziyunfei/archive/2012/11/05/2754156.html The explanation of Object.prototype.toString is clear.
The method of type recognition in JS