JavaScript: co to jest „to”?
cześć ,
W tym artykule postaramy się wyjaśnić różne możliwe wartości thissłowa kluczowego w zależności od kontekstu, w jakim jest ono używane w JavaScript.
Zakres globalny
W zasięgu globalnym thisodnosi się do obiektu globalnego ( windoww sieci iw globalNode.js).
globalThismoże być również używany jako niezależny od platformy sposób dotarcia do globalnego obiektu.
// global scope (web)
console.log(this === window); // true
console.log(this === globalThis); // true
Wewnątrz funkcji innej niż strzałka wartość thiswill zależy od sposobu wywołania funkcji.
Wywoływana jako samodzielna funkcja
- W trybie nieścisłym odnosi się do obiektu globalnego:
this
function getThis() {
return this;
}
console.log(getThis() === globalThis); // true
"use strict"
function getThis() {
return this;
}
console.log(getThis() === undefined); // true
const bar = {
getThis: function() {
return this;
}
};
console.log(bar.getThis() === bar); // true
const baz = {};
baz.getThis = bar.getThis;
console.log(baz.getThis() === baz); // true
// Called as a standalone function
const getThis = bar.getThis;
console.log(getThis() === globalThis); // true (`false` in strict mode)
Metody klasowe zachowują się tak samo jak metody w literałach obiektowych.
Funkcje strzałek
Wewnątrz funkcji strzałki wartość this jest równa wartości otaczającego kontekstu leksykalnego .
Innymi słowy, oceniając funkcję strzałki, silnik JS nie tworzy nowego thispowiązania, ale używa tego dostępnego w otaczającym zakresie :
// (1) `this` equals to `globalThis` here
const foo = {
getThis: () => this
};
console.log(foo.getThis() === foo); // false
console.log(foo.getThis() === globalThis); // true (1)
// (1) `this` equals to `globalThis` here
const bar = {
getArrowFn: function() {
// (2) `this` equals to `bar` or `baz` here
// depending on how the function is called below
return () => this;
}
};
console.log(bar.getArrowFn()() === bar); // true (2)
console.log(bar.getArrowFn()() === globalThis); // false (1)
const baz = {};
baz.getArrowFn = bar.getArrowFn;
console.log(baz.getArrowFn()() === baz); // true (2)
console.log(baz.getArrowFn()() === globalThis); // false (1)
Konstruktory funkcji
Wewnątrz funkcji używanej jako konstruktor (ze newsłowem kluczowym) thisjest powiązana z konstruowanym nowym obiektem, bez względu na sposób dostępu do funkcji na:
function Bar() {
this.baz = 42;
}
let a = new Bar();
console.log(a.baz); // 42
const bar = {
baz: function() {
this.foo = 42;
}
};
let a = new bar.baz();
console.log(a.foo); // 42
console.log(bar.foo); // undefined
function Bar() {
this.baz = 42;
return { baz: 44 };
}
let a = new Bar();
console.log(a.baz); // 44 (not 42)
zastosuj(), wywołaj()
Metod and można użyć do jawnego ustawienia wartości call()w funkcjach innych niż strzałki:apply()this
function getThis() {
return this;
}
const bar = {
getThis: function() {
return this;
}
};
console.log(getThis() === globalThis); // true
console.log(bar.getThis() === bar); // true
const myThis = {};
console.log(getThis.call(myThis) === myThis); // true
console.log(getThis.apply(myThis) === myThis); // true
console.log(bar.getThis.call(myThis) === myThis); // true
console.log(bar.getThis.apply(myThis) === myThis); // true
W przypadku użycia w funkcji strzałki thisparametr jest ignorowany (jednak nadal można przekazywać argumenty funkcji za pomocą tych metod):
// (1) `this` equals to `globalThis` here
const getThis = () => this;
console.log(getThis() === globalThis); // true
const myThis = {};
console.log(getThis.call(myThis) === globalThis); // true
console.log(getThis.apply(myThis) === globalThis); // true
Funkcja bind()służy do tworzenia nowej funkcji, która jest powiązana z danym obiektem.
Oznacza to, że po wywołaniu nowej funkcji thissłowo kluczowe będzie odnosić się do obiektu, z którym jest powiązane:
function getThis() {
return this;
}
console.log(getThis() === globalThis); // true
const myThis = {};
const getMyThis = getThis.bind(myThis);
console.log(getMyThis() === myThis); // true
console.log(getMyThis.call(globalThis) === myThis); // true
console.log(getMyThis.apply(globalThis) === myThis); // true
W przypadku użycia w funkcji strzałki thisparametr jest ignorowany (jednak nadal można przekazywać argumenty funkcji za pomocą tych metod):
// (1) `this` equals to `globalThis` here
const getThis = () => this;
console.log(getThis() === globalThis); // true
const myThis = {};
const getMyThis = getThis.bind(myThis);
console.log(getMyThis() === globalThis); // true
console.log(getMyThis.call(myThis) === globalThis); // true
console.log(getMyThis.apply(myThis) === globalThis); // true
![Czym w ogóle jest lista połączona? [Część 1]](https://post.nghiatu.com/assets/images/m/max/724/1*Xokk6XOjWyIGCBujkJsCzQ.jpeg)



































