Typescript keyword that always refer to the current instance of the class

3540 views typescript
5

it is possible in Typescript to use "this" or something else meaning just "the current instance of the class" like in C# e Java? And not as the usual binding context meaning in javascript?

answered question

1 Answer

10

Inside a TypeScript class, using the keyword this actually referring to the current instance of the class. You can see this in the documentation:

class Greeter { 
    greeting: string; 
    constructor(message: string) { 
        this.greeting = message; 
    } 
    greet() { 
        return "Hello, " + this.greeting; 
    } 
} 

let greeter = new Greeter("world");

posted this

Have an answer?

JD

Please login first before posting an answer.