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.tsx212 chars11 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