Javascript propertyIsEnumerable returns false but still enumerable

前端 未结 1 823
甜味超标
甜味超标 2021-01-27 04:24

According to MDN:

The propertyIsEnumerable() method returns a Boolean indicating whether the specified property is enumerable

But w

相关标签:
1条回答
  • 2021-01-27 04:38

    propertyIsEnumerable only checks properties on the object itself, it doesn't consider the prototype chain. So:

    me.propertyIsEnumerable('info')
    

    returns false because there is no info property on me, it's on Person.prototype. It shows up in the for loop because it will iterate over all properties of me, including those inherited from its constructor's prototype.

    Because of this behaviour, propertyIsEnumerable was used as a substitute for hasOwnProperty in early versions of Safari where hasOwnProperty was either buggy or not present (I can't remember which).

    0 讨论(0)
提交回复
热议问题