getify / You-Dont-Know-JS

A book series on JavaScript. @YDKJS on twitter.
Other
178.28k stars 33.42k forks source link

this & object prototypes, Setting & Shadowing Properties #1663

Closed YaroslavTER closed 4 years ago

YaroslavTER commented 4 years ago

Please type "I already searched for this issue": I already searched for this issue

Edition: 1st

Book Title: this & object prototypes

Chapter: 5

Section Title: Setting & Shadowing Properties

Question: In the text mentioned that [[Prototype]] traversed, just like for the [[Get]] operation.

If foo is not already present directly on myObject, the [[Prototype]] chain is traversed, just like for the [[Get]] operation.

But doesn't it performs like [[Set]] operation? I just don't get it. Or then why it similar to [[Get]]?

getify commented 4 years ago

[[Set]] is what it's called now, but back then when the book was written, it was called [[Put]]. As the text explains, [[Put]] has to traverse the prototype chain like [[Get]] does, because of the checks it performs (described in the numbered-bullet list a few paragraphs after the sentence you quoted). A lot of people assume that [[Put]] / [[Set]] just operate directly on the object, and don't realize that properties/accessors higher up the prototype chain can affect their behavior.