ES5 added a number of methods to Object
, which seem to break the semantic consistency of JavaScript.
For instance, prior to this extension, the JavaScript API always revolved around operarting on the object itself;
var arrayLength = [].length;
var firstPosInString = "foo".indexOf("o");
... where as the new Object methods are like;
var obj = { };
Object.defineProperty(obj, {
value: 'a',
writable: false
});
... when the following would have been much more conformative:
var obj = { };
obj.defineProperty({
value: 'a',
writable: false
});
Can anyone cool my curiosity as to why this is? Is there any code snippets that this would break? Are there any public discussions made by the standards committee as to why they chose this approach?
This is not correct. E.g.
JSON
andMath
always had own methods. Nobody does such things:There are numerous articles on the web about why extending
Object.prototype
is a bad thing. Yes, they're about client code, but maybe this is bad for build-in methods also for some points.This is all explained very nicely in "Proposed ECMAScript 3.1 Static Object Functions: Use Cases and Rationale" document (pdf) by Allen Wirfs-Brock himself (editor of ES5 spec, and a member of TC39).
I would suggest to read all of it. It's pretty short, easily digestible, and gives a nice glimpse of the thought process behind these ES5 additions.
But to quote relevant section (emphasis mine):