Skip to content

hasOwnProperty: 检测某一个属性是否为当前对象的私有属性

in: 检测某一个属性是当前对象的属性(不管公有还是私有)

js
// 检测某一个属性是否为当前对象的公有属性
function hasOwn (prop) {
  return Object.prototype.hasOwnProperty(prop)
}

Object.prototype.hasPub = function (prop) {
  const has = prop in this // 存在该属性
  const isOwn = hasOwn(prop) // 是否是私有

  return has && !isOwn
}