hasOwnProperty 判断对象是否具有某属性的注意点

155 阅读1分钟

最好使用 Object.prototype.hasOwnProperty.call(obj, 'prop') 而不是 obj.hasOwnProperty('prop')

有三方面的原因:

  • If obj inherits from null, not Object.prototype
  • If hasOwnProperty has been redeclared on obj
  • If hasOwnProperty has been redeclared in obj's prototype chain

2022

Object.hasOwn(),主要目标是用来替代Object.prototype.hasOwnProperty()。目前本提案已经进去第四阶段,预计2022年纳入标准。