JavaScript to determine whether a variable is an array, function, or object type, javascript Array
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.
Copy codeThe 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]'.
Copy codeThe 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 '.
Copy codeThe 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.