hasOwnProperty vs propertyIsEnumerable

后端 未结 4 1109
执念已碎
执念已碎 2020-12-29 05:52

Can anyone enlighten me, what is the difference between hasOwnProperty and propertyIsEnumerable:

4条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-29 06:06

    The difference is that propertyIsEnumerable returns true only if the property exists and if it possible to do ForIn on the property, hasOwnProperty will return true if the property exists regardless to ForIn support

    From MSDN:

    The propertyIsEnumerable method returns true if proName exists in object and can be enumerated using a ForIn loop. The propertyIsEnumerable method returns false if object does not have a property of the specified name or if the specified property is not enumerable. Typically, predefined properties are not enumerable while user-defined properties are always enumerable.

    The hasOwnProperty method returns true if object has a property of the specified name, false if it does not. This method does not check if the property exists in the object's prototype chain; the property must be a member of the object itself.

提交回复
热议问题