ES6-01: Create classes and instances

constructor

The constructor() method is the constructor (default method) of the class. It is used to pass parameters and return the instance object. This method is automatically called when the object instance is generated by the new command. If the definition is not displayed, the class will always create a constructor() for us.

  • As long as the new object, it will automatically call the constructor of the class.
  • The first letter of the class name is generally capitalized.

Code

<script>
    class Star {
     
     
        constructor(name) {
     
     
            this.name = name;
        }
    }
    var test = new Star("测试名");
    console.log(test.name);
</script>

Guess you like

Origin blog.csdn.net/sinat_41696687/article/details/113836081