由于constructor是可以被赋值改变的,如https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Object/constructor
因此前两种方法并不靠谱,因此推荐第三四种。
1. instanceof方法
function isArray(a) {
return a instanceof Array;}
2. constructor 方法
function isArray(a) {
return a.constructor == Array;
}
3. 特性判断法
function isArray(a){
return a && typeof a==='object' &&
typeof a.length==='number' &&
typeof a.splice==='function' &&
!(a.propertyIsEnumerable('length'));
}
4. call方法
function isArray(a){
return Object.prototype.toString.call(a) === '[object Array]';
}
测试:
function t() {
var a = [];
console.log(isArray(arguments), isArray(a))
}
t();
参考:http://www.cnblogs.com/mofish/p/3388427.html 、 http://blog.csdn.net/zhangw428/article/details/4171630