Issue
This Content is from Stack Overflow. Question asked by GoodMan
I am reading a code like below:
export abstract class CustomError extends Error {
abstract statusCode: number;
constructor(message: string) {
super(message);
Object.setPrototypeOf(this, CustomError.prototype);
}
abstract serializeErrors(): { message: string; field?: string }[];
}
I can’t understand what does this
refer to? I think in this case this
should be equal to CustomError.prototype
, and if so, this expression would be meaning less to me:
Object.setPrototypeOf(this, CustomError.prototype);
Because it’s like:
Object.setPrototypeOf(this, this);
Solution
this reffers to the variable itself. If I made a class,
class thing{
constructor() {console.log(this)}
}
new thing();
"this" reffers to the class.
This Question was asked in StackOverflow by GoodMan and Answered by BREgitLEEhub2222 It is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.