Sub topic 15
Background: Why the Methods Are Static
This chapter assumes you have used Object.groupBy and Map.groupBy from earlier chapters. You should also know what Array.prototype methods are and how libraries can add methods to built-in prototypes. Nothing here changes how you write grouping code. It explains why you write Object.groupBy(items, fn) and not items.groupBy(fn).
The Original Plan: An Array Method
Most array helpers live on Array.prototype, so you call them on the array itself: items.map(fn), items.filter(fn). The grouping proposal began the same way. Its first draft added Array.prototype.groupBy, which returned an object, and Array.prototype.groupByToMap, which returned a Map.
That design matched existing habits, but it ran into a long-standing JavaScript rule: new built-in features must not break websites that already work. Browser vendors call this web compatibility.
Why Prototype Names Collide
Older libraries often patched built-in prototypes. Some sites contain code like this:
if (!Array.prototype.groupBy) {
Array.prototype.groupBy = function (key) {
// a library's own version, with its own signature and return shape
const out = {};
for (const item of this) {
(out[item[key]] ||= []).push(item);
}
return out;
};
}This guard installs the library version only when no native method exists. Once browsers ship a native groupBy, the guard skips the library code and the page calls the native method. The native version expects a callback, not a property name, and returns a different shape, so the page breaks. When browsers tested the proposal, they found exactly this conflict with the Sugar.js library and with sites that used it.
The committee renamed the methods to group and groupToMap. Those names also clashed with existing code on real sites. Each new prototype name carried the same risk, because any widely deployed script might already use it.
The Fix: Static Methods on Object and Map
The final design put the methods on the constructors: Object.groupBy and Map.groupBy. A static method is a function stored on the constructor itself, not on instances. Old libraries rarely patched Object or Map with a groupBy function, so the collision risk dropped sharply. The proposal reached Stage 4 in late 2023 and became part of ES2024.
Side Benefits of the Static Design
The move fixed the naming problem, and it also brought two design gains.
| Benefit | What it means in practice |
|---|---|
| Works on any iterable | Strings, Sets, and generators can be grouped without converting them to arrays first |
| Return type is visible | `Object.groupBy` returns an object, `Map.groupBy` returns a Map, and the namespace tells you which |
You can confirm both points in any ES2024 runtime:
console.log(typeof Array.prototype.groupBy); // "undefined"
console.log(typeof Object.groupBy); // "function"
const tags = new Set(["js", "css", "json"]);
const byLetter = Map.groupBy(tags, (tag) => tag[0]);
console.log([...byLetter.keys()]); // ["j", "c"]Exercise
A legacy page defines Array.prototype.groupBy only when it is missing. Explain why shipping a native Array.prototype.groupBy would break that page, but shipping Object.groupBy does not.
Show answer
The guard skips the library code once a native method exists, so the page starts calling the native version, which has a different signature and return shape. Object.groupBy is a new property on the Object constructor and does not touch Array.prototype, so the library's method still runs unchanged.
Exercise
Name one practical advantage of the static design beyond avoiding name collisions.
Show answer
It accepts any iterable, such as a Set or string, without converting it to an array first. Another valid answer: the namespace (Object or Map) shows what type the result will be.