tc39 / proposal-accessible-object-hasownproperty Goto Github PK
View Code? Open in Web Editor NEWObject.hasOwn() proposal for ECMAScript
Home Page: https://tc39.es/proposal-accessible-object-hasownproperty/
License: MIT License
Object.hasOwn() proposal for ECMAScript
Home Page: https://tc39.es/proposal-accessible-object-hasownproperty/
License: MIT License
Which use cases of objects-as-dictionaries are not better handled by using Map
s?
For the symmetry purpose, can we have shorthands for all the methods defined on Object.prototype
?
Rationale:
We need a counterpart to Object.keys()
and Object.values()
. (Object.hasOwn()
being a counterpart to Reflect.ownKeys()
).
I think checking has-own&enumerable is a far more common use-case than just has-own. I have seen lots of flawed code in the wild that assumes hasOwnProperty
is a counterpart to Object.keys()
. See: facebook/react#19921
Suggestions for a better name are welcome... propertyIsEnumerable
is rather awkardly named.
Here's a polyfill:
Object.hasOwnEnumerable = Function.prototype.call.bind(Object.prototype.propertyIsEnumerable);
I perfectly understand that has
is more frequent name than hasOwn
for this shortcut, but we have Reflect.has
which works like in
operator. The rest Reflect
methods work similarly to Object
methods - we should avoid this inconsistency. Also, I think, the method name should point to check own properties.
Edit from @jamiebuilds:
This proposal was presented to TC39 on April 20, 2021, where it went forward to Stage 2 but the name was left in question. This issue should be used for discussing the method name further.
How about publishing the package to let folks make use of the polyfill? You already have the main
field, etc. Thanks!
Using this issue as a central place to collect community feedback for in-the-field experience, positive or negative.
hasOwn
ToPropertyKey
throws before ToObject
, but the wording implies it's for legacy compatibility, and there is no previous Object.hasOwn
. In other words, if it's important to make the function truly be identical to Object.prototype.hasOwnProperty
, the note needs better wording; however, it seems better to me to do the ToObject first?As the faq why name has
, "it fills in a hole in the common operations between Object, Map, and Set."
So I assume the new API should keep the semantic consistency with keys/values methods.
Map/Set all satisfy the invariant of: collection.has(key) === [...collection.keys()].includes(key)
If we also want the invariant of Object.has(o, key) === Object.keys(o).includes(key)
, the semantic of has()
should only return true
for enumerable string keys, which is different with Object.prototype.hasOwnProperty
.
Object.hasOwn
enabled in nightly for the last month (as of July 2021) and has not received any bug reports (https://bugzilla.mozilla.org/show_bug.cgi?id=1711872#c8)Object.hasOwn()
in Chrome 93 in late August.A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.