add a property to a class in javascript

To add a property to a class in JavaScript, you need to define the property inside the class using the this keyword. Here's an example:

index.tsx
class MyClass {
  constructor() {
    this.myProperty = 'Hello World';
  }
}

// Creating an instance of MyClass
const myClassInstance = new MyClass();

// Accessing the property
console.log(myClassInstance.myProperty); // Output: Hello World
243 chars
12 lines

In this example, we define a class MyClass and add a property myProperty inside the class constructor using the this keyword. We then create an instance of MyClass and access the myProperty property using the dot notation.

gistlibby LogSnag