class继承
class
关键字是在ES6引入的
ES6之前的写法:
function Student(name) {this.name = name
}
// 给Student新增一个方法
Student.prototype.hello = function () {alert('Hello')
}
ES6的写法:
// 定义一个 学生的 类
class Student1{constructor(name) {this.name = name}hello(){alert('hello')}
}
let xiaoming = new Student1('xiaoming')
console.log(xiaoming.name)
xiaoming.hello()
继承:
class Student1{constructor(name) {this.name = name}hello(){alert('hello')}
}
class xiaoStudent extends Student1{constructor(name, grade) {super(name);this.grade = grade}myGrade(){alert("我是一名小学生")}
}
let xiaoStu = new xiaoStudent('xiaoming1', 2)
console.log(xiaoStu.name) // xiaoming1
console.log(xiaoStu.grade) // 2
console.log(xiaoStu)
xiaoStu.myGrade()
class的写法和之前的写法本质是一样的,只是写法不同
原型链:
https://www.cnblogs.com/loveyaxin/p/11151586.html
https://www.bilibili.com/video/BV1JJ41177di?p=18