类数组变成真数组的方法

1.array.from:2.展开运算符:3.array.prototype.slice.call()

判断数组类型:

①最好用array.isarray()其余判断方式有时会报错

Array.isArray() 用于检查一个值是否为数组类型。它会返回一个布尔值。

Array.isArray([]);  // true

Array.isArray({});  // false

②typeof :

typeof 42;  // "number"

typeof 'Hello';  // "string"

typeof true;  // "boolean"

typeof undefined;  // "undefined"

typeof null;  // "object"(这是一个历史遗留问题,实际上 null 是一个特殊的对象类型)

typeof [];  // "object"(数组被视为对象)

typeof {}   // "object"

typeof function() {}  // "function"                        

③instanceof 用于检查一个对象是否属于某个特定的对象类型。

let arr = [];

let obj = {};

console.log(arr instanceof Array);  // true

console.log(obj instanceof Object);  // true

④Object.prototype.toString.call(); 可以返回一个表示对象类型的字符串。

Object.prototype.toString.call(42);  // "[object Number]"

Object.prototype.toString.call('Hello');  // "[object String]"

Object.prototype.toString.call(true);  // "[object Boolean]"

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

Object.prototype.toString.call({});  // "[object Object]"

Object.prototype.toString.call(function() {});  // "[object Function]"

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值