Constructor ()

The constructor() method is the class’s constructor(default method), used to pass arguments, return an instance object, and is automatically called when an instance of the object is generated using the new command. If no definition is shown, we are always given a constructor() inside the class.

  • Whenever a new object is created, the constructor of the class is automatically called.
  • The first letter of a class name is usually capitalized.

code

<script>
    class Star {
        constructor(name) {
            this.name = name; }}var test = new Star("Test name");
    console.log(test.name);
</script>
Copy the code