Skip to content
On this page

如何判断数组类型?

js
// 方法一:instanceof方法
let arr = [1, 2, 3];
console.log(arr instanceof Array);

// 方法二:constructor方法
let arr = [1, 2, 3];
console.log(arr.constructor === Array);

// 方法三:isArray方法
let arr = [1, 2, 3];
console.log(Array.isArray(arr));

// 方法四:Object.prototype方法
let arr = [1, 2, 3];
console.log(Object.prototype.toString.call(arr) === '[object Array]');

// 方法五:Array.__proto__方法
let arr = [1, 2, 3];
console.log(arr.__proto__ === Array.prototype);

// 方法六:Object.getPrototypeOf方法
let arr = [1, 2, 3];
console.log(Object.getPrototypeOf(arr) === Array.prototype);

// 方法七:Array.prototype.isPrototypeOf方法
let arr = [1, 2, 3];
console.log(Array.prototype.isPrototypeOf(arr));

Released under the MIT License.