admin管理员组

文章数量:1287776

In this video (approx. 31 minutes in), Crockford says they (speaking on behalf of the ECMAScript mittee) remend not using Object.getPrototypeOf. His explanation was that it wasn't really meant for the average developer but was meant for things like Caja, which may remove it from Object to prevent you from accessing it.

Crockford can sometimes be pretty opinionated in his views on how JS should be used (can't we all?), so I'm wondering if this is really the full remendation of the ES mittee or if it's just one of Crockford's personal opinions. Has anyone read any official statement warning against using Object.getPrototypeOf? It really sounds like a bummer to me :(, but I don't see any information on the MDN page warning against its use, and I would expect a notice to be there if it really was that bad of an idea.

In this video (approx. 31 minutes in), Crockford says they (speaking on behalf of the ECMAScript mittee) remend not using Object.getPrototypeOf. His explanation was that it wasn't really meant for the average developer but was meant for things like Caja, which may remove it from Object to prevent you from accessing it.

Crockford can sometimes be pretty opinionated in his views on how JS should be used (can't we all?), so I'm wondering if this is really the full remendation of the ES mittee or if it's just one of Crockford's personal opinions. Has anyone read any official statement warning against using Object.getPrototypeOf? It really sounds like a bummer to me :(, but I don't see any information on the MDN page warning against its use, and I would expect a notice to be there if it really was that bad of an idea.

Share Improve this question edited Oct 22, 2012 at 20:21 Nathan Wall asked Sep 30, 2012 at 4:35 Nathan WallNathan Wall 10.7k4 gold badges28 silver badges48 bronze badges 2
  • 1 I haven't got time to write a full answer just now, but a couple of things I've found: 1. __proto__ is not obviously a huge security issue, but Brendan Eich was against adding it because it's non-standard and exposes an unnecessary attack surface (Mario Heiderich expands a little upon the security implications in his doctoral thesis); and 2. Douglas first made this claim in definitive terms back in 2008. – Jordan Gray Commented Oct 4, 2012 at 14:27
  • This question is argumentative and will mainly provide opinionated answers. There's not right/wrong here. Although valid concerns but I should be formulated differently. – Robert Koritnik Commented Oct 23, 2012 at 13:34
Add a ment  | 

1 Answer 1

Reset to default 15 +450

His reasoning there is incredibly poor. It (and Object.getOwnPropertyNames) were not simply added for the use of Caja and similar. Nor does Caja simply delete them! Caja intercepts Object.getOwnPropertyNames in order to implement WeakMap (which my shim does as well) and as far as I can tell it doesn't modify getPrototypeOf. In reality it would be pointless to anyway because Object.getPrototypeOf(o) is the same thing as o.__proto__ which is implemented in every browser aside from IE and can't (currently) be turned off. That means the only browsers that removing Object.getPrototypeOf from would have any effect on are IE9 and IE10.

The reason I figured he'd give is that some of those functions are mostly intended for use by "library author" type usages. This is something monly believed/said by people involved with the specification process and I believe it is a legitimate claim; property descriptors/attributes and other "meta" level API's are more advanced features that can be cumbersome to use and generally require more plete language mastery to use correctly. However, this still wouldn't amount to a blanket remendation of "don't use them". This more accurate claim wasn't even the argument he made, though.

One extra note about the video, in which he made a incorrect statement. He said property attributes (enumerable, configurable, writable) were unchangeable once set. This is incorrect. These can be changed so long as configurable is true. Once it is set to false the attributes bee frozen (nor is the property deletable).


Edit: After having done research, I found some of the original discussions regarding this feature and the other Object functions. The summary as I understand it follows.

There was concern about the security implications of being able to access the [[Prototype]] of an object. However, these concerns were more fully and appropriately addressed via things like Object.freeze, and is also partly addressed (and a reason) that these functions live on Object as static functions (deletable in one location) instead of on Object.prototype or magically on every object like proto historically has been.

Another concern raised was of breaking encapsulation

It's true that proto or getPrototypeOf breaks an object's encapsulation barrier and reveals implementation details that perhaps were intended to be hidden. The same could be said about the proposed getProperty function which, among other things, gives an observer access to the functions that implement a getter/setter property. In general, that's the nature of reflection. -Allen Wirfs-Brock

One concern raised from the implementation end was about exposing implementation details (mostly a concern stemming from how the DOM works which has since been addressed by changes to the DOM's use of multiple inheritance and the transition to WebIDL).

On the other hand, providing reflective access to an object's prototype is harmful to patibility because it prevents implementations from introducing intermediate prototypes without breaking the web. Consider the example of having just Numbers and later patibly introducing more specific subkinds of Numbers. -Waldemar Horwat

This concern is also related to another one mentioned on the script coordination mailing list about internal hidden prototypes being the same cross-frame. This issue is also historical as of ES5 (and IE8) where it was decided and implemented that each frame must instantiate its own set of DOM prototypes. Thus hiding of prototypes for this reason was no longer relevant by the time ES5 was formally published.


The consensus I see doesn't follow Crockford's explanation. Mostly it seems to just be the restatement of his own opinion.

In summary, not providing reflective access to an object's prototype doesn't really provide any real security, it just makes some useful tasks less convenient. -Allen Wirfs-Brock


I agree with you here in general, and it's good to hear that reflection is not the enemy of "real security". -Brendan Eich

The starting point for this is Proposed ECMAScript 3.1 Static Object Functions: Use Cases and Rationale (written by Crockford and others on TC39). The followup to that, where I draw quotations from, is this es-discuss thread. Specifically this post and this post.

本文标签: javascriptDon39t use getPrototypeOfStack Overflow