Can anyone enlighten me, what is the difference between
hasOwnProperty
and propertyIsEnumerable
:
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.