JavaScript中的构造方法用于创建和初始化在类中创建的对象。如果未添加构造函数方法,则应使用默认构造函数。
注–类中仅允许出现一次构造函数方法。一个以上引发错误。
您可以尝试运行以下代码以了解如何实现构造函数方法
<html> <body> <script> class Department { constructor() { this.name = "Department"; } } class Employee extends Department { constructor() { super(); } } class Company {} Object.setPrototypeOf(Employee.prototype, Company.prototype); document.write("<br>"+Object.getPrototypeOf(Employee.prototype) === Department.prototype); let myInstance = new Employee(); document.write("<br>"+myInstance.name); </script> </body> </html>