call a public method on an instance of a class in javascript

To call a public method on an instance of a class in JavaScript, you can use dot notation to access the method on the instance. Here's an example:

index.tsx
class MyClass {
  constructor() {
    // constructor code here
  }
  
  myMethod() {
    console.log('Hello from myMethod!');
  }
}

const myInstance = new MyClass();

myInstance.myMethod(); // outputs "Hello from myMethod!"
225 chars
14 lines

In this example, we define a class MyClass with a public method myMethod. We then create a new instance of the class, myInstance, using the new keyword. Finally, we call the myMethod method on myInstance using dot notation, which outputs "Hello from myMethod!" to the console.

Note that the method must be defined as a public method (i.e. not inside the constructor) in order to be accessed on the instance using dot notation.

gistlibby LogSnag