判断数组

判断数组
1.通过Array.isArray()判断
Array.isArray()用于确定传递的值是否是一个数组,返回一个布尔值

let a = [7,8,9];
Array.isArray(a);  //true

2.通过instanceof判断
instanceof运算符用于检验构造函数的prototype属性是否出现在对象的原型链中的任何位置,返回一个布尔值

let a = [];
a instanceof Array; //true
let b = {};
b instanceof Array; //false
//instanceof 运算符检测Array.prototype属性是否存在于变量a的原型链上
//显然a是一个数组,拥有Array.prototype属性,所以为true

3..通过constructor判断
实例的构造函数属性constructor指向构造函数,通过constructor属性可以判断是否为一个数组

let a = [7,8,9];
a.constructor === Array;  //true

4.通过Object.prototype.toString.call()判断
Object.prototype.toString.call()可以获取到对象的不同类型

let a = [7,8,9];
Object.prototype.toString.call(a) === '[Object Array]';  //true
posted @ 2023-11-20 20:34  张尊娟  阅读(6)  评论(0编辑  收藏  举报