js class定义类,私有属性,类继承,子类访问父类的方法,重写父类的方法
1.使用#定义私有属性
2.使用extends实现类继承
3.使用子类使用super访问父类的方法
4.子类可以重新父类的方法
class People {#hisroy = []getHistroy() {return [...this.#hisroy]}addHisroy(message) {this.#hisroy.push({role: 'user',message,})this.addHisroy2(message)}addHisroy2(message) {this.#hisroy.push({role: 'robot',message: 'Robot:' + message,})}addHisroy3(message) {this.#hisroy.push({role: 'user',message,})}clearHistory() {this.#hisroy = []}}class Student extends People {addHisroy(message) {if (message.includes('weather')) {super.addHisroy3(message)super.addHisroy2('天气晴朗')} else {super.addHisroy(message)}}}let p1 = new People()p1.addHisroy('hello')console.log(p1.getHistroy())p1.clearHistory()console.log(p1.getHistroy())let t1 = new Student()t1.addHisroy('hello')console.log(t1.getHistroy())t1.addHisroy('what is the weather?')console.log(t1.getHistroy())t1.clearHistory()console.log(t1.getHistroy())
https://www.kancloud.cn/fundebug/fundebug-blog/1039647
https://jamie.build/javascripts-new-private-class-fields.html