fixes
This commit is contained in:
parent
eeee9fedf7
commit
7697f95e24
4 changed files with 41 additions and 38 deletions
|
@ -14,11 +14,11 @@ let user = makeUser();
|
|||
alert( user.ref.name ); // Error: Cannot read property 'name' of undefined
|
||||
```
|
||||
|
||||
That's because rules that set `this` do not look at object literals.
|
||||
That's because rules that set `this` do not look at object definition. Only the moment of call matters.
|
||||
|
||||
Here the value of `this` inside `makeUser()` is `undefined`, because it is called as a function, not as a method.
|
||||
Here the value of `this` inside `makeUser()` is `undefined`, because it is called as a function, not as a method with "dot" syntax.
|
||||
|
||||
And the object literal itself has no effect on `this`. The value of `this` is one for the whole function, code blocks and object literals do not affect it.
|
||||
The value of `this` is one for the whole function, code blocks and object literals do not affect it.
|
||||
|
||||
So `ref: this` actually takes current `this` of the function.
|
||||
|
||||
|
@ -42,5 +42,3 @@ alert( user.ref().name ); // John
|
|||
```
|
||||
|
||||
Now it works, because `user.ref()` is a method. And the value of `this` is set to the object before dot `.`.
|
||||
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue