ES6学习笔记(二):教你玩转类的继承和类的对象
继承 程序中的继承: 子类可以继承父类的一些属性和方法 class Father { //父类 constructor () { } money () { console.log(100) } } class Son extends Father { //子类继承父类 } let son = new Son() son.money() // 100 son. super关键字 super关键字用于访问和调用对象父类上的函数,可以通过调用父类的构造函数,也可以调用父类的普通函数 class Father { //父类 constructor (x, y) { this.x = x this.y = y } money () { console.log(100) } sum () { console.log(this.x + this.y) } } class Son extends Father { //子类继承父类 constructor (x, y) { super(x, y) //调用了父类中的构造函数 } } let son = new Son(1,2) son.sum() // 3 son. 继承的特点: 继承中,如果实例化子类输出一个方法,先看子类有没有这个方法,如果有就先执行子类,(就近原则) 继承中,如果子类里面没有,就去查找父类有没有这个方法,如果有