多種方法判斷一個對象是否包含一個屬性

前言

判斷一個對象是否包含一個屬性有多種的方式,如下:

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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章