Because that's so much simpler than using Object.create(null)?
What's going to stop the next person from using methods of this object? That doing that was a mistake will only become apparent when a collision happens.
You are trading issues which are easy to find and immediately pop up for potential issues which only show up if very specific conditions are met.
Failing early is better.
And you seem to be saying you’ve never actually used Object.create(null) that way anyway.
As I said, I've used Object.create(null) for creating dictionary-like objects where the keys are user-supplied. E.g. something like a mime type in an archive format or a request header. Those strings could be anything. Collisions might be deliberate.
So what is an actual reason you actually used this pattern? Also, have you heard of Maps?
It's like Map<string, whatever>, but you can use square brackets. It's a convenient lightweight option if all of your keys are strings.
As I said, I've used Object.create(null) for creating dictionary-like objects where the keys are user-supplied. E.g. something like a mime type in an archive format or a request header.
Okay. Cool. Seems like in the post ES6 era a Map is the better solution. Unless I am missing something?
In my experience, convenience (or "simplicity", an oft-abused term) should be third or fourth priority when writing code. First priority (after satisfying the AC), should be clarity. Imagine being a new developer on a project, calling an object method on what appeared to be an object, and then spending the better part of a day tracking the bizarre errors that resulted.
A Map is clear. hasOwnProperty is clear. My little hasProp one-liner? Clear. If a new developer does not understand how it works from context, they know to look up the function definition. Everything is perfectly transparent. Object.create(null) just seems like setting a booby trap for future developers.
Seems like in the post ES6 era a Map is the better solution. Unless I am missing something?
Yes, Map<K, V> can store anything a dictionary-like object (<string, V>) can store. You certainly can use that instead.
However, it's a bit slower and you can't use [] or []= (like you can with Dart's Maps, for example).
hasOwnProperty is clear.
But it wouldn't work. It also isn't obvious that it won't. That should be pretty high on your list, right?
Object.create(null) just seems like setting a booby trap for future developers.
This is one of the two ways to use Object.create. It isn't anything special. A JS developer should roughly know how prototypes work. This crap is a very important detail of this language.
1
u/inu-no-policemen May 15 '19
Because that's so much simpler than using Object.create(null)?
What's going to stop the next person from using methods of this object? That doing that was a mistake will only become apparent when a collision happens.
You are trading issues which are easy to find and immediately pop up for potential issues which only show up if very specific conditions are met.
Failing early is better.
As I said, I've used Object.create(null) for creating dictionary-like objects where the keys are user-supplied. E.g. something like a mime type in an archive format or a request header. Those strings could be anything. Collisions might be deliberate.
It's like
Map<string, whatever>, but you can use square brackets. It's a convenient lightweight option if all of your keys are strings.