On Mon, Dec 16, 2013 at 3:24 PM, Oliver Joseph Ash
<[email protected]> wrote:
> I'm noticing that many methods added in ES5 and due in ES6 are defined on
> the type's constructor function instead of on the type's prototype. For
> example, Object.keys, Object.defineProperty, and Array.isArray.
>
> Is there a reason these were not added to the prototype, i.e.
> Object.prototype.keys, for consistency?

The Object methods are on the constructor because if they were put on
the prototype, they'd show up on every single object in all of JS.
The potential for property-name collision would be terrible.

Some methods, like isArray(), aren't very useful if they only show up
on the prototype - being able to ask an array if it's an Array is
nice, but you'd like to be able to ask *any* object.  Either you
instead define Object.prototype.isArray() (and that still doesn't help
primitives, or objects with a null prototype), or you stash it
somewhere as a plain function, rather than a method.  The Array
constructor is a convenient and memorable place to do so - it serves
as an adequate namespace for the method to avoid polluting the global
object.

~TJ
_______________________________________________
es-discuss mailing list
[email protected]
https://mail.mozilla.org/listinfo/es-discuss

Reply via email to