2

JavaScript noob here. I understand that in general, in choosing to define a method in the constructor vs the prototype, the constructor approach allows for the use of closures by taking advantage of local variables defined in the constructor, as described here:

Declaring javascript object method in constructor function vs. in prototype

Does a similar reasoning apply for standard built-in objects? For ex. I'm looking through MDN documentation for the Object constructor and I see methods defined within both the constructor and the prototype. What were reasons for designing these standard object's methods to be defined in either the constructor or the prototype?

Community
  • 1
  • 1
Greg
  • 193
  • 1
  • 1
  • 11
  • Welcome to [so]! It seems your question is the same as [Javascript: why Object.keys(someobject), rather than someobject.keys?](http://stackoverflow.com/questions/11922619/javascript-why-object-keyssomeobject-rather-than-someobject-keys) Please let me know if this is not the case. – Qantas 94 Heavy Feb 16 '15 at 03:27
  • Thanks! The answers to that post bring up a good point about the consequences of adding shadowing properties to `Object.prototype`, which adds one reason to the solution to my post, but the rest of the answers seem more specific to `Object.keys`. I'm curious to know of any more reasons if they do exist. – Greg Feb 16 '15 at 03:45
  • Another question that's very similar is [Why were ES5 Object methods not added to Object.prototype?](http://stackoverflow.com/questions/9735026/why-were-es5-object-methods-not-added-to-object-prototype) – Qantas 94 Heavy Feb 16 '15 at 03:50
  • 1
    methods defined "on" a constructor are static methods that can be easily applied to non-instances. – dandavis Feb 16 '15 at 04:07

0 Answers0