【无标题】

 判断对象为空的方法

1.使用 JSON.stringify() 方法

对象是引用型数据 无法直接比较

const obj = {};
const str = JSON.stringify(obj);
const isEmpty = (str === '{}');

2.使用 Object.keys() 方法

Object.keys() 静态方法返回一个由给定对象自身的可枚举的字符串键属性名组成的数组。

const obj = {};
const keys = Object.keys(obj);
const isEmpty = (keys.length === 0);

 判断是数组

1.Array.isArray() 方法

const arr = [1, 2, 3];
console.log(Array.isArray(arr)); // true

2.instanceof Array

  • 检查对象的原型链中是否包含 Array.prototype
    const arr = [1, 2, 3];
    console.log(arr instanceof Array); // true

 判断是否是字符串

1.使用 typeof 运算符

const data='123'
typeof data === "string"

2.使用 constructor 属性

  • 每个JavaScript对象都有一个 constructor 属性,指向创建它的构造函数。
    const str = "Hello, world!";
    console.log(str.constructor === String); // true

 3.使用 instanceof 运算符

const str = "Hello, world!";
console.log((new String(str)) instanceof String); // true

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值