前言
判斷一個對象是否包含一個屬性有多種的方式,如下:
const obj = {
name: 'john'
}
var a = 'name' in obj
var b = Reflect.has(obj, 'name')
var c = Reflect.hasOwnProperty.call(obj, 'name')
var d = Object.prototype.hasOwnProperty.call(obj, 'name')
console.log(a, b, c, d) // true true true true