js-如何判断数组是类型

1.用typeof运算符来判断

2.用instanceof判断

instanceof运算符可以用来判断某个构造函数的prototype属性所指向的對象是否存在于另外一个要检测对象的原型链上。

在使用的时候语法如下: object instanceof constructor

3.用constructor判断 console.log(a.constructor == Array);//true

4.用Object的toString方法判断 const a = ['Hello','Howard'];

Object.prototype.toString.call(a);

//"[object Array]" Object.prototype.toString.apply(a);

//"[object Array]"

总结:

const isArray = (something)=>{ return Object.prototype.toString.call(something) === '[object Array]'; }

cosnt a = [];

const b = {}; isArray(a);//true isArray(b);//false

5.用Array对象的isArray方法判断

const a = []; const b = {}; Array.isArray(a);//true Array.isArray(b);//false

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值