1- ** Answer: an error .**
1+ ** Resposta: um erro .**
22
3- Try it :
3+ Experimente :
44``` js run
55function makeUser () {
66 return {
@@ -14,26 +14,26 @@ let user = makeUser();
1414alert ( user .ref .name ); // Error: Cannot read property 'name' of undefined
1515```
1616
17- That's because rules that set ` this ` do not look at object definition. Only the moment of call matters .
17+ Isso é porque as regras que definem ` this ` não olham para a definição do objeto. Apenas o momento da chamada importa .
1818
19- Here the value of ` this ` inside ` makeUser() ` is ` undefined ` , because it is called as a function, not as a method with "dot" syntax .
19+ Aqui o valor de ` this ` dentro de ` makeUser() ` é ` undefined ` , porque ela é chamada como uma função, não como um método com sintaxe de "ponto" .
2020
21- The value of ` this ` is one for the whole function, code blocks and object literals do not affect it .
21+ O valor de ` this ` é único para toda a função, blocos de código e literais de objeto não o afetam .
2222
23- So ` ref: this ` actually takes current ` this ` of the function .
23+ Então ` ref: this ` na verdade obtém o ` this ` atual da função .
2424
25- We can rewrite the function and return the same ` this ` with ` undefined ` value:
25+ Podemos reescrever a função e retornar o mesmo ` this ` com valor ` undefined ` :
2626
2727``` js run
2828function makeUser (){
29- return this ; // this time there's no object literal
29+ return this ; // desta vez não há literal de objeto
3030}
3131
3232alert ( makeUser ().name ); // Error: Cannot read property 'name' of undefined
3333```
34- As you can see the result of ` alert( makeUser().name ) ` is the same as the result of ` alert( user.ref.name ) ` from the previous example .
34+ Como você pode ver, o resultado de ` alert( makeUser().name ) ` é o mesmo que o resultado de ` alert( user.ref.name ) ` do exemplo anterior .
3535
36- Here's the opposite case :
36+ Aqui está o caso oposto :
3737
3838``` js run
3939function makeUser () {
@@ -52,4 +52,4 @@ let user = makeUser();
5252alert ( user .ref ().name ); // John
5353```
5454
55- Now it works, because ` user.ref() ` is a method. And the value of ` this ` is set to the object before dot ` . ` .
55+ Agora funciona, porque ` user.ref() ` é um método. E o valor de ` this ` é definido como o objeto antes do ponto ` . ` .
0 commit comments