In the current Ecmascript 6 draft (November 2012), there is official support for the __proto__
property (Section B.3.1). This came as a bit of a surprise as, in current browser implementations, the __proto__
property is deprecated.
If the current draft stands, will ES6 have full support for mutable prototypes? Will I be able to provide a new prototype for an existing object the way I currently can in Firefox?
In the current Ecmascript 6 draft (November 2012), there is official support for the __proto__
property (Section B.3.1). This came as a bit of a surprise as, in current browser implementations, the __proto__
property is deprecated.
If the current draft stands, will ES6 have full support for mutable prototypes? Will I be able to provide a new prototype for an existing object the way I currently can in Firefox?
Share Improve this question edited Dec 12, 2012 at 11:54 Jack Wester asked Dec 12, 2012 at 11:49 Jack WesterJack Wester 5,6503 gold badges30 silver badges48 bronze badges 5-
The
__proto__
property is "deprecated", because its not a standard property, insteadObject.getPrototypeOf(someObject);
is to be preferred (ECMA5 std, thus X-browser support). Other than that, changing a prototype on-the-fly is not exactly new: I tried it in chrome and IE, changing the prototype of a custom object tonew Array
, without any issues... it's just inherent to the prototype model – Elias Van Ootegem Commented Dec 12, 2012 at 11:55 - @Elias. Object.getPrototypeOf() is an Ecmascript 5 feature as _ _ proto _ _ was never part of the Ecmascript standard. It appears that _ _ proto _ _ was first deprecated from the Browsers and then introduced (for the first time) in the standard. Although this order of events appear unusual, this appears to be the case. I'm asking specifically about the "new" _ _ proto _ _ in Ecmascript 6 - B.3.1. – – Jack Wester Commented Dec 12, 2012 at 12:12
- I'm not sure what you're asking. It's in the current draft, so yes, it looks like we'll have that. On the other hand, it's still a draft, so anything can change. – bfavaretto Commented Dec 12, 2012 at 12:31
- @bfavaretto - The question is if the ES6 _ _ proto _ _ will provide the same mutable behaviour as the older deprecated versions. I.e. is this a plete resurrection of the assumed to be dead _ _ proto _ _ implementation found in some browsers. – Jack Wester Commented Dec 12, 2012 at 14:47
-
2
In addition to @benvie's wonderful answer, also note that mutable [[Prototype]] exists in the current draft as
Reflect.setPrototypeOf
. There has been discussion about that being incorrectly placed, and it sounds like__proto__
will end up being used instead, but it's still a little uncertain at this point. – Nathan Wall Commented Dec 15, 2012 at 7:15
2 Answers
Reset to default 9Currently, it is planned for mutable __proto__
to be in the spec (and not just in annex b). The current plan is for it to be a magical data property existing solely on Object.prototype and acting as an accessor (the magical part). This property will be deletable as well, removing the ability to mutate __proto__
for that realm when deleted. I've implemented __proto__
following this description in my ES6 virtual machine http://benvie.github./continuum.
Originally it was to be deprecated and replaced, but no consensus formed on that replacement was. The ability to inherit from builtins is the end goal and the decision was that __proto__
is already widespread (everything but ie has it) and fulfills this goal, so the path of least resistance was to embrace and codify it.
As per Ecmascript 6 released docs, __proto__
still exists but as Bergi and MattBrowne pointed in the above solution, we can use Object.setPrototypeOf
which is the modifed version of __proto__
.
When the setPrototypeOf function is called with arguments O and proto, the following steps are taken:
- Let O be RequireObjectCoercible(O).
- ReturnIfAbrupt(O).
- If Type(proto) is neither Object nor Null, throw a TypeError exception.
- If Type(O) is not Object, return O.
- Let status be O.[SetPrototypeOf].
- ReturnIfAbrupt(status).
- If status is false, throw a TypeError exception.
- Return O.