Nhảy tới nội dung

Tham số this (this parameter)

Tham số đầu tiên của function (ngoài arrow function) và method của class có thể nhận tham số đặc biệt là this. Vì ý nghĩa của this thay đổi tùy theo context sử dụng, nên điều này được dùng để cho TypeScript biết function này nên được sử dụng trong context nào. Khi gọi, caller không cần quan tâm đến this này. Chỉ cần chỉ định từ tham số thứ hai trở đi.

ts
class Male {
private name: string;
 
public constructor(name: string) {
this.name = name;
}
 
public toString(): string {
return `Monsieur ${this.name}`;
}
}
 
class Female {
private name: string;
 
public constructor(name: string) {
this.name = name;
}
 
public toString(this: Female): string {
return `Madame ${this.name}`;
}
}
ts
class Male {
private name: string;
 
public constructor(name: string) {
this.name = name;
}
 
public toString(): string {
return `Monsieur ${this.name}`;
}
}
 
class Female {
private name: string;
 
public constructor(name: string) {
this.name = name;
}
 
public toString(this: Female): string {
return `Madame ${this.name}`;
}
}

Class MaleFemale ở trên có cấu trúc gần như giống nhau nhưng tham số của method toString() khác nhau.

MaleFemale đều có thể sử dụng theo cách thông thường.

ts
const male: Male = new Male("Frédéric");
const female: Female = new Female("Frédérique");
 
male.toString();
Monsieur Frédéric
female.toString();
Madame Frédérique
ts
const male: Male = new Male("Frédéric");
const female: Female = new Female("Frédérique");
 
male.toString();
Monsieur Frédéric
female.toString();
Madame Frédérique

Tuy nhiên, khi gán toString() của mỗi instance vào biến, ý nghĩa sẽ thay đổi.

ts
const maleToStr: () => string = male.toString;
const femaleToStr: (this: Female) => string = female.toString;
 
maleToStr();
femaleToStr();
The 'this' context of type 'void' is not assignable to method's 'this' of type 'Female'.2684The 'this' context of type 'void' is not assignable to method's 'this' of type 'Female'.
ts
const maleToStr: () => string = male.toString;
const femaleToStr: (this: Female) => string = female.toString;
 
maleToStr();
femaleToStr();
The 'this' context of type 'void' is not assignable to method's 'this' of type 'Female'.2684The 'this' context of type 'void' is not assignable to method's 'this' of type 'Female'.

femaleToStr() nhận được cảnh báo rằng context không phải là Female. Code này không thể thực thi được. Nhân tiện, maleToStr() không có xử lý này tuy có thể thực thi nhưng sẽ xảy ra exception runtime.

js
class Male {
// ...
toString() {
return `Monsieur ${this.name}`;
TypeError: Cannot read property 'name' of undefined
}
}
js
class Male {
// ...
toString() {
return `Monsieur ${this.name}`;
TypeError: Cannot read property 'name' of undefined
}
}

Bằng cách chỉ định tham số this, có thể tránh việc mang method ra ngoài context không mong muốn.