由于 js 中数组属于引用类型,而引用类型都归为 object,所以数组是不能通过 typeof 来判断

typeof Array; // output: object

因此需要通过其它手段来判断

  • 原型判断
  • 实例的父类判断
  • ES5 中的 isArray 方法
  • 构造函数判断
  • 使用 Object.prototype 判断
var arr = [];
console.log(
  arr.__proto__ === Array.prototype,
  arr instanceof Array,
  Array.isArray(arr),
  arr.constructor === Array,
  Object.prototype.toString.call(arr).slice(8, 13) === 'Array'
);
// output: true true true true true