Object constructor function got several methods during the past updates of js like apply, assign, entries, fromEntries, keys, values...
These would be excellent candidates to be included in object prototype.
Object.prototype.values = function(f) {
return Object.values(this)
}
We could even combine them to implement map or filter:
Object.prototype.map = function(f) {
return Object.fromEntries(Object.entries(this).map(f))
}
// now we could do...
obj1 = {a:1, b:2}
obj1.values() // [1,2]
obj1.map([a,b] => ['x'+a: b+1]) // {xa:2, xb:3}
This syntax would have been unquestionably superior compared to eg.
Object.fromEntries(Object.entries(obj1).map([a,b] => ['x'+a: b+1])))
But even
obj1.entries()
.map([a,b]=>['x'+a: b+1])
.fromEntries()
is much more readable.
Backward compatibility doesn't seem to be a problem, since further objects in the prototype chain would mask the methods (eg. Array.prototype.map would still work properly).
Other route was taken, and there must be a technical reason. I'm pretty much curious what it is.
Are there any examples (possibly in legacy code) where the above approach would fail?