The problem with Object methods being on the prototype is that objects in general can have arbitrary key-value pairs. For example, if there was such a thing as Object.prototype.values
, it could very easily cause confusion when you had an object which you intended to have a values
property, eg
const foo = { values: ['val1', 'val2'] };
If Object.prototype.values
were a thing, to use it here, you would have to do something like
const fooValues = Object.prototype.values.call(foo);
Better for values
to be a static method to avoid such name collisions.
If the properties of your objects could be functions as well, it could be even worse, for example:
const foo = {
info: ['a', 'b', 'c'],
values() {
return this.info;
}
};
Now, if Object.prototype.values
was a thing, and you saw foo.values()
in the code, what do you think it would mean? Was the programmer intending to call the values
property of foo
(which is what the code would result in), or was the programmer intending to use Object.prototype.values
? It'd be an easy source of bugs and hard-to-read code.
Same thing for the other Object static methods - they could easily result in name collisions.