Suggestion: "Object.hasOwnProperty"

# doodad-js Admin (8 years ago)

I think to create "Object.hasOwnProperty". Often, an object is used to store key/value pairs and most users forget that "hasOwnProperty" can be also be a key. Objects created by "Object.create(null)" don't inherit this function and users get lost when they get such an object from a library or their own code.

Claude Petit

# Lewis Ellis (8 years ago)

Object.hasOwnProperty already exists via the prototype chain, but I'm guessing you wish it did something different.

Both the concern of someObj.hasOwnProperty(somePropertyName) attempting to call the hasOwnProperty property of someObj rather than walking the prototype chain to get Object.prototype.hasOwnProperty, and the concern of someObj having a null prototype (or otherwise not having Object.prototype.hasOwnProperty on its prototype chain) can be avoided with the {}.hasOwnProperty.call(someObj, somePropertyName) pattern.

# doodad-js Admin (8 years ago)

Both the concern of someObj.hasOwnProperty(somePropertyName) attempting to call the hasOwnProperty property of someObj rather than walking the prototype chain to get Object.prototype.hasOwnProperty, and the >> concern of someObj having a null prototype (or otherwise not having Object.prototype.hasOwnProperty on its prototype chain) can be avoided with the {}.hasOwnProperty.call(someObj, somePropertyName) pattern.

Hi,

Yeah, sure. But it could be cleaner to do:

Object.hasOwnProperty(myObj, someName)

instead of

{}.hasOwnProperty.call(myObj, someName)
# Michał Wadas (8 years ago)

This would break a Web, a I have seen code that relies on Object.hasOwnProperty === Object.prototype.hasOwnProperty

Anyway, it would be handy to have such function as Reflect.hasOwn or something like that.

# doodad-js Admin (8 years ago)

This would break a Web, a I have seen code that relies on Object.hasOwnProperty === Object.prototype.hasOwnProperty

Sorry, I didn’t remember that the constructor inherits it.

Anyway, it would be handy to have such function as Reflect.hasOwn or something like that.

Yeah, maybe that then. Or “System.ObjectHasOwn” or else.

# Isiah Meadows (8 years ago)

I'd, for ergonomic reasons, would prefer it on Reflect. I very frequently alias this function, so having it as a static method on Reflect would be nice.

# Jordan Harband (8 years ago)

Reflect is only for API mirrors to Proxy traps - that's why Reflect.enumerate was removed along with the Enumerate Proxy trap.