1. typeof
typeof return value is a string, there are six types
Number,string,boolean,function,undefined,object,
typeof are commonly used to differentiate between undefined and function types, and cannot distinguish between complex types, such as array type, and plain object type
Use typeof (revalue) = = = "undefined" to distinguish undefined, not revalue = = undefined to distinguish undefined, because revalue is not declared, the browser directly error
Use typeof (revalue) = = = "function" to detect function
2. instanceof
Instanceof is used to determine the relationship between an object instance and a type, the return value is of type bool and cannot span an IFRAME.
3. Constructor
Constructor detection is using the object's constructor property, whose value is a reference to the constructor of that object type. For example, the value of Num.constructor is function number () {native code}. This method can be used to determine the array type, such as Obj.constructor = = = Array to determine whether the type is an array.
The constructor method has some drawbacks, one is that the constructor attribute can be modified, and the other is that constructor's judgment cannot pass through the IFRAME.
4. Object.prototype.toString.call
This method invokes a specific object that returns a total of 11 types, as follows
' [Object number] ' [Object String] ' [Object Boolean] ' [Object Array] ' [Object Object] '
' [Object Function] ' [Object Undefined] ' [Object Null] ' [Object Date] ' [Object RegExp] ' [Object Error] '
This method is the ultimate method used to detect types, and the jquery type () method is used to determine the use of this method, the specific use of the type of judgment, you can solve the $.type implementation.
Summary: Use typeof to detect undefined and function, other types of detection using Tosting.call or third-party encapsulated type methods when used specifically.
Reference: http://www.2cto.com/kf/201507/414817.html
JavaScript Data type judgment