call a method on an instance of a class in javascript

To call a method on an instance of a class in JavaScript, we first need to create an instance of the class using the 'new' keyword. Then we can call the method on the instance using the 'dot' notation.

Here is an example:

index.tsx
class Person {
  constructor(name) {
    this.name = name;
  }
  
  sayHello() {
    console.log(`Hello, my name is ${this.name}.`);
  }
}

const john = new Person('John');
john.sayHello(); // outputs "Hello, my name is John."
227 chars
13 lines

In this example, we create an instance of the Person class called 'john'. We then call his 'sayHello' method using dot notation, which logs a message to the console. This is the basic syntax for calling a method on an instance of a class in JavaScript.

gistlibby LogSnag