7 ms·
What's the thinking behind attaching Object.groupBy to the Object constructor? With the other Object.X() functions I can easily see why it makes sense they exis
by aragonite 3y ago
What's the thinking behind attaching Object.groupBy to the Object constructor? With the other Object.X() functions I can easily see why it makes sense they exist on the Object constructor, because they generally follow the pattern of taking an object as argument, and say, freezes that object, returns that object's [[Prototype]], returns that object's keys, check if that object is sealed, etc. By contrast, Object.groupBy is a utility function that takes an iterable and a callback, and doesn't seem to have anything distinctively to do with "Object" per se.
(I guess one exception is Object.is, which takes two values as arguments. But even Object.is makes a lot of sense existing on the Object constructor because it exposes an important abstract operation (SameValue). Object.groupBy is only a utility function)
- danvk 3y agoWhat about Object.fromEntries? See https://github.com/tc39/proposal-array-grouping https://github.com/tc39/proposal-array-grouping for why this isn’t a method on Array.prototype.
- flqn 3y agoConstructs an object from kv pairs, definitely assoctiated with the "Object" prototype. Groupby is more dubious
- deleted 3y ago[deleted]
- LegionMammal978 3y agoMap.groupBy() returns a Map with the keys mapped to array values. In parallel, Object.groupBy() returns an Object with the keys cast into property names. Thus, I'd say it fits the pattern of Object-related static methods, since it's using the Object mechanism as a map.
- zeroimpl 3y agoThe part I’m confused on is the documentation saying the returned object from Object.groupBy doesn’t extend from Object.prototype. Is it still an object? The return value of groupBy is an object that does not inherit from %Object.prototype%.
- edflsafoiewq 3y agoIt's called a null-prototype object: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object#null-prototype_objects https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe... They're "usually used as a cheap substitute for maps": the lack of prototype prevents the object from being polluted by keys that morally shouldn't be there, like toString, etc.
- aragonite 3y agoIt's a value that belongs as a member of the Object data type (Object(it)===it), but it's not (in addition) an instance of the Object constructor (!(it instanceof Object)). I guess the former, wider sense of 'an object' is more often intended.
- bakkoting 3y agoWe originally tried to put it on Array.prototype, under multiple different names, and that broke various pages in various ways. So we gave up and had to put it somewhere else. And it's conceptually similar to `fromEntries` - they're both ways of making an object. So the Object constructor was the obvious choice.
- replygirl 3y agoit's a noble approach, but feels like it's at its limits if no one could find a reasonable name that works with Array. there's gotta be a point at which it makes sense to EOL third-party stuff like that: set a date far in the future, and any pages where document.lastModified is greater get Array.groupBy as well
- bakkoting 3y agoNot gonna happen: https://github.com/tc39/faq?tab=readme-ov-file#why-dont-we-just-break-the-web https://github.com/tc39/faq?tab=readme-ov-file#why-dont-we-j... Browsers aren't going to break old pages.
- Silhouette 3y agoBrowsers have broken old pages on a massive scale on several occasions. Numerous useful pages were effectively lost when they decided to shut down widely used plugins like Flash and Java applets that were mainstays of the interactive web for years. Google/Chrome in particular are notorious for pushing features before they're properly standardised, allowing developers to start relying on them, and then pulling them because they are no longer convenient. A better answer here might be to give the language a version declaration feature so browsers could reliably provide a backward-compatible interpretation of old scripts if necessary. If breaking changes are only made when there is great value in the change and no reasonable alternative then they shouldn't happen often enough for this kind of versioning to become an unmanageable burden.
- bakkoting 3y ago