Reflect.has和Object.hasOwnProperty的区别
使用示例
console.log(Reflect.has(obj, 'foo'))
/*
* 根据最新的eslint官方默认规则,不推荐:obj.hasOwnProperty
* 而推荐使用:Object.prototype.hasOwnProperty.call
*/
console.log(obj.hasOwnProperty('foo'))
console.log(Object.prototype.hasOwnProperty.call(obj, 'foo'))
共同点
都能检查某个对象是否包含某个属性。
不同点
Reflect.has
会检查对象的所有属性,而`hasOwnProperty`不会检查对象继承的属性。如果变量`obj`不是一个对象,`Reflect.has`会报错,而`hasOwnProperty`不会报错
Reflect.has
要求支持`ES6`,而`hasOwnProperty`从`ES3`开始就已经支持了
Reflect.has(1, 'foo')
// throw Error: Uncaught TypeError: Reflect.has called on non-object
Object.prototype.hasOwnProperty.call(1, 'foot')
// echo: false