简体   繁体   中英

Does Ecmascript 6 support a mutable __proto__ property?

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?

Currently, 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.com/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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM