<script>
// 构造函数
// function student1(name,age,marks){
// this.name=name;
// this.age=age;
// this.marks=marks;
// }
// student1.prototype.display=function(){
// console.log(this.name,this.age,this.marks);
// }
// var student3=new student("sai",23,90);
// student3.display();
// class 类
class student {
constructor(name, age, marks) {
this.name = name;
this.age = age;
this.marks = marks;
}
display() {
console.log(this.name, this.age, this.marks);
}
//vscode调整格式快捷键 Shift + Alt + F
//student类包含了构造函数 constructor和包含在原型对象上的 display函数,少了function
}
var student4 = new student("sai", 13, 40);
student4.display();
// 然后继承,语法格式和java中的差不多,要用到extends关键字
class student2 extends student {
constructor(name, age, marks, rollno) {
super(name, age, marks);// super调用父类的构造方法
this.rollno = rollno;
}
display2() {
console.log(this.rollno);
}
}
var student5 = new student2("sai", 13, 40, 1);
student5.display();//sai 13 40
student5.display2();// 1
</script>