Skip to content

Latest commit

 

History

History
55 lines (40 loc) · 1.42 KB

File metadata and controls

55 lines (40 loc) · 1.42 KB

Odpowiedź: błąd.

Uruchom ten kod:

function makeUser() {
  return {
    name: "John",
    ref: this
  };
};

let user = makeUser();

alert( user.ref.name ); // Error: Cannot read property 'name' of undefined

Jest to spowodowane tym, że reguły ustalające wartość this nie uwzględniają faktu istnienia obiektu. Znaczenie ma tylko moment wywołania.

Wartość this wewnątrz makeUser() jest undefined, ponieważ jest wywołana jako funkcja, a nie jako metoda wywołana za pomocą "kropki".

Wartość this jest tu ustalona wyłącznie dla tej funkcji. Bloki kodu i obiekty nie są w tym przypadku brane pod uwagę.

Zatem ref: this jest równoznaczne z this funkcji.

Możemy napisać tę funkcję od nowa w taki sposób, że będzie zwracała takie samo this z wartością undefined:

function makeUser(){
  return this; // tym razem nie jest zwracany obiekt
}

alert( makeUser().name ); // Error: Cannot read property 'name' of undefined

Jak widzisz wynik alert( makeUser().name ) jest taki sam jak wynik alert( user.ref.name ) z poprzedniego przykładu.

A tutaj odwrotna sytuacja:

function makeUser() {
  return {
    name: "John",
*!*
    ref() {
      return this;
    }
*/!*
  };
};

let user = makeUser();

alert( user.ref().name ); // John

Teraz kod działa prawidłowo, ponieważ user.ref() jest metodą, a wartością this jest obiekt przed kropką ..