Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
That's because `this` is an object before the dot, so `rabbit.eat()` modifies `rabbit`.

Property lookup and execution are two different things.
The method `rabbit.eat` is first found in the prototype, then executed with `this=rabbit`

The method `rabbit.eat` is first found in the prototype, then executed with `this=rabbit`.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Let's look carefully at what's going on in the call `speedy.eat("apple")`.

So all hamsters share a single stomach!

Every time the `stomach` is taken from the prototype, then `stomach.push` modifies it "at place".
Both for `lazy.stomach.push(...)` and `speedy.stomach.push()`, the property `stomach` is found in the prototype (as it's not in the object itself), then the new data is pushed into it.

Please note that such thing doesn't happen in case of a simple assignment `this.stomach=`:

Expand Down Expand Up @@ -77,4 +77,4 @@ alert( speedy.stomach ); // apple
alert( lazy.stomach ); // <nothing>
```

As a common solution, all properties that describe the state of a particular object, like `stomach` above, are usually written into that object. That prevents such problems.
As a common solution, all properties that describe the state of a particular object, like `stomach` above, should be written into that object. That prevents such problems.
6 changes: 2 additions & 4 deletions 1-js/08-prototypes/01-prototype-inheritance/article.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,6 @@ The method is automatically taken from the prototype, like this:

The prototype chain can be longer:


```js run
let animal = {
eats: true,
Expand Down Expand Up @@ -128,11 +127,10 @@ Now if we read something from `longEar`, and it's missing, JavaScript will look
There are only two limitations:

1. The references can't go in circles. JavaScript will throw an error if we try to assign `__proto__` in a circle.
2. The value of `__proto__` can be either an object or `null`, other types (like primitives) are ignored.
2. The value of `__proto__` can be either an object or `null`. Other types are ignored.

Also it may be obvious, but still: there can be only one `[[Prototype]]`. An object may not inherit from two others.


```smart header="`__proto__` is a historical getter/setter for `[[Prototype]]`"
It's a common mistake of novice developers not to know the difference between these two.

Expand Down Expand Up @@ -178,7 +176,7 @@ From now on, `rabbit.walk()` call finds the method immediately in the object and

![](proto-animal-rabbit-walk-2.svg)

That's for data properties only, not for accessors. If a property is a getter/setter, then it behaves like a function: getters/setters are looked up in the prototype.
Accessor properties are an exception, as assignment is handled by a setter function. So writing to such a property is actually the same as calling a function.

For that reason `admin.fullName` works correctly in the code below:

Expand Down