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

chore: use Object.hasOwn() #749

Merged
merged 1 commit into from
Aug 15, 2024
Merged

chore: use Object.hasOwn() #749

merged 1 commit into from
Aug 15, 2024

Conversation

EvanHahn
Copy link
Contributor

This change should have no user impact.

Object.hasOwn() replaces Object.prototype.hasOwnProperty() in modern JavaScript. Its advantages can be seen in the following code snippet:

// Still works even if you overwrite `hasOwnProperty`:
const overwritten = { hasOwnProperty: () => true }
Object.hasOwn(overwritten, "foo")
// => false
overwritten.hasOwnProperty("foo")
// => true (incorrect)

// Still works even if the object has a null prototype:
const nullPrototype = Object.create(null)
nullPrototype.foo = "bar"
Object.hasOwn(nullPrototype, "foo")
// => true
nullPrototype.hasOwnProperty("foo")
// TypeError: nullPrototype.hasOwnProperty is not a function

This replaces all uses of Object.prototype.hasOwnProperty() with Object.hasOwn().

I think this is a useful change on its own, but will also help in a future change.

This change should have no user impact.

`Object.hasOwn()` replaces `Object.prototype.hasOwnProperty()` in modern
JavaScript. Its advantages can be seen in the following code snippet:

```
// Still works even if you overwrite `hasOwnProperty`:
const overwritten = { hasOwnProperty: () => true }
Object.hasOwn(overwritten, "foo")
// => false
overwritten.hasOwnProperty("foo")
// => true (incorrect)

// Still works even if the object has a null prototype:
const nullPrototype = Object.create(null)
nullPrototype.foo = "bar"
Object.hasOwn(nullPrototype, "foo")
// => true
nullPrototype.hasOwnProperty("foo")
// TypeError: nullPrototype.hasOwnProperty is not a function
```

This replaces all uses of `Object.prototype.hasOwnProperty()` with
`Object.hasOwn()`.

I think this is a useful change on its own, but will also help in a
future change.
@@ -1,7 +1,7 @@
{
"compilerOptions": {
"target": "es2019",
"lib": ["es2020"],
"lib": ["es2022"],
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Object.hasOwn works in our version of Node, so I believe this is a safe change to make.

@EvanHahn EvanHahn merged commit 1fc0795 into main Aug 15, 2024
7 checks passed
@EvanHahn EvanHahn deleted the Object.hasOwn branch August 15, 2024 15:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants