Array
In ECMAScript5, Array.isArray is a native method to judge arrays, supported by IE9 and above. In view of compatibility, in browsers without this method, you can use Object.prototype.toString.call(obj) === '[object Array]' instead.
The code copy is as follows:
var isArray = Array.isArray || function(obj) {
return Object.prototype.toString.call(obj) === '[object Array]';
}
function
The easiest and best-performance method is typeof obj == 'function'. Considering the bugs in some versions of browsers, the most reliable way is Object.prototype.toString.call(obj) === '[object Function]'.
The code copy 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. Using typeof for the above two, you can get 'object' and 'function' respectively. In addition, the null value must be ruled out, because typeof null also gets 'object'.
The code copy is as follows:
var isObject = function(obj) {
var type = typeof obj;
return type === 'function' || type === 'object' && !!obj;
}
The above is all about this article, I hope you like it.