constructor关键字用于声明构造函数方法。
类只能包含一个构造方法。
JavaScript允许我们通过super关键字使用父类构造函数。
<script>
class Employee {
constructor() {
this.id=101;
this.name = "Martin Roy";
}
}
var emp = new Employee();
document.writeln(emp.id+" "+emp.name);
</script>
<script>
class CompanyName
{
constructor()
{
this.company="lidihuo";
}
}
class Employee extends CompanyName {
constructor(id,name) {
super();
this.id=id;
this.name=name;
}
}
var emp = new Employee(1,"John");
document.writeln(emp.id+" "+emp.name+" "+emp.company);
</script>