call a property on an instance of a class in typescript

To call a property on an instance of a class in TypeScript, you first need to create a new instance of the class using the new keyword.

Assuming you have a class Person with a property name, the following code creates a new instance of Person and sets the name property to "John":

index.ts
class Person {
  name: string;

  constructor(name: string) {
    this.name = name;
  }
}

const john = new Person("John");
124 chars
10 lines

To call the name property on the john instance, simply use dot notation:

index.ts
console.log(john.name); // output: "John"
42 chars
2 lines

This will output the value of the name property, which is "John".

gistlibby LogSnag