Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using __proto__ in object literals is not deprecated #3014

Closed
wants to merge 1 commit into from

Conversation

bakkot
Copy link

@bakkot bakkot commented May 17, 2022

The __proto__ syntax (as opposed to the getter/setter) was moved out of Annex B in tc39/ecma262#2125. It's fine to use, and in fact is generally preferable to Object.create() (because it makes it easier to set up other normal data properties, and because it's syntax rather than an API call).

@CLAassistant
Copy link

CLAassistant commented May 17, 2022

CLA assistant check
All committers have signed the CLA.

@smith558
Copy link
Member

@bakkot Is this not the consensus of the discussion you are referring to?

image

@bakkot
Copy link
Author

bakkot commented Jun 16, 2022

@smith558 as that screenshot says, the use of the __proto__ syntax is not discouraged. It's only the use of the __proto__ accessor which is problematic.

@smith558
Copy link
Member

@smith558 as that screenshot says, the use of the __proto__ syntax is not discouraged. It's only the use of the __proto__ accessor which is problematic.

Yeah, Could you explain to me what they mean by "proto syntax"? I understand the other 2 points but for me, those would be included under "proto syntax".

@bakkot
Copy link
Author

bakkot commented Jun 16, 2022

{ __proto__: null } is special syntax (search for "isProtoSetter" in that section); it's a different thing than { foo: null }. That specific syntax is given meaning by the spec.

By contrast, obj.__proto__ is a normal property access, just like obj.foo. But there happens to be a getter/setter named __proto__ on Object.prototype, so if obj inherits from Object.prototype then this triggers the behavior of that getter/setter (just like how every other getter/setter works).

- [Object.getPrototypeOf(obj)](mdn:js/Object/getPrototypeOf) -- returns the `[[Prototype]]` of `obj`.
- [Object.setPrototypeOf(obj, proto)](mdn:js/Object/setPrototypeOf) -- sets the `[[Prototype]]` of `obj` to `proto`.

These should be used instead of `__proto__`.
These should be used instead of `.__proto__`.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Change to obj.__proto__? Clashes with above paragraph when it's referenced as obj.__proto__ rather.

@smith558
Copy link
Member

smith558 commented Jun 16, 2022

{ __proto__: null } is special syntax (search for "isProtoSetter" in that section); it's a different thing than { foo: null }. That specific syntax is given meaning by the spec.

By contrast, obj.__proto__ is a normal property access, just like obj.foo. But there happens to be a getter/setter named __proto__ on Object.prototype, so if obj inherits from Object.prototype then this triggers the behavior of that getter/setter (just like how every other getter/setter works).

I see. Thanks! I feel like Objects and Prototypes are very important concepts and a first-time reader may feel confused by seeing suggestions to "use" __proto__ and "not use" __proto__ (tho not in the same way) at once and the article changes proposed as it at this time do not fully convey the meaning and the differences in such way so as not to confuse the reader or teach them to use it correctly. (feedback)

@iliakan
Copy link
Member

iliakan commented Jun 18, 2022

You mean that using __proto__ in {...} is ok, but get/set .__proto__ is not, right?

@iliakan
Copy link
Member

iliakan commented Jun 23, 2022

@bakkot

@iliakan iliakan closed this in c20debc Jun 28, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants