Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeError: obj.hasOwnProperty is not a function #184

Closed
sgoll opened this issue Jun 27, 2017 · 1 comment
Closed

TypeError: obj.hasOwnProperty is not a function #184

sgoll opened this issue Jun 27, 2017 · 1 comment

Comments

@sgoll
Copy link
Contributor

sgoll commented Jun 27, 2017

In certain situations, compare fails to calculate a valid JSON patch and throws a TypeError with this message instead:

obj.hasOwnProperty is not a function

In my case, this happens when running objects created by GraphQL's graphql method through fast-json-patch. One location where this error originates is this line in json-patch-duplex.js:

if (obj.hasOwnProperty(key) && !(obj[key] === undefined && oldVal !== undefined && Array.isArray(obj) === false)) {

In fact, this error will be thrown whenever custom objects are used that do not have Object in their prototype hierarchy, such as plain maps created with Object.create(null).

I propose to replace all calls to special Object methods with Object.prototype calls such as this:

// Old:
obj.hasOwnProperty(key)
// Replace with:
Object.prototype.hasOwnProperty.call(obj, key)

This ensures that no exception is thrown in case objects without the Object prototype are used.

One may certainly argue that this seems like a pretty far-fetched corner case, but e.g. ESLint has a special rule against this type of Object prototype builtins: Disallow use of Object.prototypes builtins directly (no-prototype-builtins), see their rationale.

I would be willing to prepare a pull request for the required changes.

@alshakero
Copy link
Collaborator

Closing as done and released! Thank you @sgoll 🎉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants