This article mainly introduces how to judge whether a variable is an array, function, or object type in JavaScript. if you need it, refer
Array
In ECMAScript5, Array. isArray is a native method for judging arrays. IE9 and later support. Considering compatibility, you can use Object. prototype. toString. call (obj) = '[object Array]' in browsers without this method.
The code is as follows:
Var isArray = Array. isArray | function (obj ){
Return Object. prototype. toString. call (obj) = '[object Array]';
}
Function
The simplest and best-performing method is typeof obj = 'function '. Considering the bugs in some browsers, the most reliable method is Object. prototype. toString. call (obj) = '[object Function]'.
The code is as follows:
Var isFunction = function (obj ){
Return Object. prototype. toString. call (obj) = '[object Function]';
}
If (typeof /./! = 'Function' & typeof Int8Array! = 'Object '){
IsFunction = function (obj ){
Return typeof obj = 'function ';
}
}
Object
In JavaScript, complex types are objects and functions are objects. If typeof is used for the preceding two objects, 'object' and 'function' are obtained respectively '. In addition, the null value must be excluded because typeof null also produces 'object '.
The code is as follows:
Var isObject = function (obj ){
Var type = typeof obj;
Return type = 'function' | type = 'object '&&!! Obj;
}
The above is all the content in this article. I hope you will like it.