[dev-platform] Intent to prototype: Object.hasOwn

38 views
Skip to first unread message

Yulia Startsev

unread,
Jun 8, 2021, 11:53:14 AM6/8/21
to firef...@mozilla.org
Hi Everyone,

This proposal adds to JavaScript an ergonomic way to check if a property exists directly on an object, and not on it's prototype chain. It is a small proposal, but this is tricky to get right, as it will throw on objects such as Null.

A common implementation is:

let hasOwnProperty = Object.prototype.hasOwnProperty

if (hasOwnProperty.call(object, "foo")) {
  console.log("has property foo")
}

Which can now be simplified to

if (Object.hasOwn(object, "foo")) { console.log("has property foo") }


Platform coverage: All.
Preference: None, available only on Nightly.
DevTools bug: None.
Other browsers:
1. Google Chrome supports this behind a flag --harmony-object-has-own
2. Safari: Not supported yet

Test262: Tests are in progress and will be merged when they land.

best,
Yulia

Reply all
Reply to author
Forward
0 new messages