0

There are methods like Object.values fo Object.keys, but why aren't these methods inside the object's prototype? Is there a good reason for this?

Example:

const user = { name: 'John', role: 'admin' };
const keys = user.keys() // instead of Object.keys(user);
const values = user.values() // instead of Object.values(user);
Ionel Lupu
  • 2,695
  • 6
  • 29
  • 53

1 Answers1

1

Because everything is an object in JavaScript. If you add a method to the Object's prototype, it would be inherited to everything, it can't (or shouldn't, as it then hides the original method) be used as a name of a custom method. That means that if the Object.prototype would get polluted with a lot of methods, it would make the choice of property names more difficult:

  1..keys() // Did you expect this to work?
Jonas Wilms
  • 132,000
  • 20
  • 149
  • 151