create a class with a constructor in javascript

In JavaScript, you can create classes using the class keyword. A constructor is a method that is executed when an object of the class is created. Here's an example of how to create a class with a constructor in JavaScript:

index.tsx
class Person {
  constructor(name, age) {
    this.name = name;
    this.age = age;
  }
}

let person1 = new Person('John', 40);
console.log(person1.name); // Output: John
console.log(person1.age); // Output: 40
212 chars
11 lines

In the above example, we have a class Person with two properties name and age. The constructor takes two arguments name and age, and sets the class properties with those values using this. We then create an object of the class Person using the new keyword and passing in the arguments for name and age. Finally, we log the values of person1.name and person1.age to the console.

gistlibby LogSnag