-
Notifications
You must be signed in to change notification settings - Fork 20
Second argument: proto, a la Object.create, or mapper, a la Array.from? #13
Comments
This would have the opposite signature from |
That’s true. Do you think there’s a reasonable way, that doesn’t have to use |
I don't think so, though I could easily be missing something obvious. I don't think a second call (or |
Definitely not required; but it feels like it'd be a win for optimizeability and code clarity. |
The current signature is appealing to me for a two reasons:
The const instance = Object.setPrototypeOf(Object.fromEntries(entries), prototype); |
Certainly that's a solution; but that creates an object that extends Object.prototype, and then alters its prototype chain later - Firefox had a scary console warning about the performance impact of this for many years. |
If there were a second arg, one thing I’d suggest is that it seems it would cover more use cases if it were a target object to assign to, rather than a prototype to use: |
That might indeed be a better solution; but in that case, "from" might not be the proper word to use in the name - all the "from" methods generate a new object. Regardless of our choice here, I'd hope that the default and most ergonomic usage creates a new object instead of mutating an existing one. |
@bathos' idea has merit: It doesn't introduce a new way to create objects with non- The second, optional argument is essentially the The implementation is essentially: Object.fromEntries = function (entries, target = {}) {
for (const [key, value] of entries) {
target[key] = value;
}
return target;
} This does not solve the issue raised by attempting to use One (gross) possibility would be to allow the user to call I'm leaning more toward if you need a custom prototype for your object, perhaps a different function than vanilla |
I don't think we should be motivated by "passing it as a callback to map and friends"; that should be done in an arrow function to ensure explicit passing of args rather than implicit ones. |
Another use case has popped up for a second argument in #7 - that it could be a mapper function, a la |
We've decided not to support a second argument. |
In particular, this would be useful to do
Object.fromEntries(entries, null)
. It could reuse the same spec machinery as Object.create.The text was updated successfully, but these errors were encountered: