1、in运算符
用法name in obj
let obj = {
name: '张三',
age: 18,
work: '专业搬砖',
}
console.log('age' in obj);//true
console.log('sex' in obj);//false
2、Reflect.has(obj, name)
Reflect.has方法对应name in obj里面的in运算符。
Reflect.has()方法的第一个参数是对象
let obj = {
name: '张三',
age: 18,
work: '专业搬砖',
}
console.log(Reflect.has(obj, 'age'));//true
console.log(Reflect.has(obj, 'sex'));//false
3、hasOwnProperty()
用法object.hasOwnProperty(name) // true/false
hasOwnProperty() 方法是 Object 的原型方法(也称实例方法),它定义在 Object.prototype 对象之上,所有 Object 的实例对象都会继承 hasOwnProperty() 方法
let obj = {
name: '张三',
age: 18,
work: '专业搬砖',
}
console.log(obj.hasOwnProperty('age')) //true
console.log(obj.hasOwnProperty('sex'));//false