Is it spec-compliant to forbid setPrototypeOf(Object.prototype, foo)?
On Thu, Feb 2, 2017 at 12:19 PM, Michał Wadas <michalwadas at gmail.com> wrote:
Right now this code:
Object.setPrototypeOf(Object.prototype, Object.create(null))
throws in both Chrome and Firefox. I couldn't find any mention of special [[SetPrototypeOf]] of %ObjectPrototype%, did I miss something?
Yes. :-) You missed that Object.prototype
is an immutable prototype
exotic object:
- tc39.github.io/ecma262/#sec-properties-of-the-object-prototype-object
- tc39.github.io/ecma262/#sec-immutable-prototype-exotic-objects
...which does indeed have a special [[SetPrototypeOf]] which (indirectly)
returns false if you try to change the [[Prototype]].
Object.setPrototypeOf
throws when [[SetPrototypeOf]] returns false:
tc39.github.io/ecma262/#sec-object.setprototypeof
-- T.J.
Thanks, I erroneously looked at ES2015 spec that calls Object.prototype ordinary object.
The Object prototype object is the intrinsic object
%ObjectPrototype%. The Object prototype object is an ordinary object.
The value of the [[Prototype]] internal slot
<http://www.ecma-international.org/ecma-262/6.0/#sec-object-internal-methods-and-internal-slots>
of the Object prototype object is null and the initial value of the
[[Extensible]] internal slot
<http://www.ecma-international.org/ecma-262/6.0/#sec-object-internal-methods-and-internal-slots>
is true.
On Thu, Feb 2, 2017 at 12:38 PM, Michał Wadas <michalwadas at gmail.com> wrote:
Thanks, I erroneously looked at ES2015 spec that calls Object.prototype ordinary object.
The Object prototype object is the intrinsic object %ObjectPrototype%. The Object prototype object is an ordinary object. The value of the [[Prototype]] internal slot www.ecma-international.org/ecma-262/6.0/#sec-object-internal-methods-and-internal-slots of the Object prototype object is null and the initial value of the [[Extensible]] internal slot www.ecma-international.org/ecma-262/6.0/#sec-object-internal-methods-and-internal-slots is true.
Ah! I looked in the ES2016 spec to make sure it wasn't a recent change, didn't think to go back to the one before...
-- T.J.
Also, Object.freeze
causes Object.setPrototypeOf
to fail.
Right now this code:
Object.setPrototypeOf(Object.prototype, Object.create(null))
throws in both Chrome and Firefox. I couldn't find any mention of special [[SetPrototypeOf]] of %ObjectPrototype%, did I miss something?