super关键词
super
super通常用语继承中,例如:
1 class Node{ 2 constructor(element){ 3 this.element = element 4 this.next = undefined 5 } 6 log(){ 7 console.log("test") 8 } 9 } 10 11 class DoubleNode extends Node{ 12 constructor(element){ 13 super(element) 14 this.prev = undefined 15 } 16 log(){ 17 console.log("test2") 18 } 19 test(){ 20 this.log() 21 super.log() 22 } 23 }
构造函数中:
super相当于吧父类的constructor执行了,并将this转为子类的实例,super中传递的实参实际是给父类传递的
constructor(element){ super(element) this.prev = undefined }
还可通过super调用父类的原形方法:
node = new DoubleNode(1) node.test()
结果为
test2
test