class Father{
constructor(){
this.name = 'father'
this.age = 33
}
work(){
console.log('I'm the parent'); }}class Children extends Father{
constructor(name,age,play){
super(a)this.name = name
}
}
let personZhang = new Children('tony'.44.'Play games')
console.log(personZhang.name);
Copy the code
super
/ / by extends
// super keyword
// Inheritance must call super from the constructor method
// The reason is that the subclass's own this object must be generated by the parent class's constructor
// You can't get this object without calling a super subclass
class A{
// How to write attributes??
pA = 123
p(){
return 3
}
}
A.prototype.pA = 123
class B extends A{
constructor(){
super(a)console.log(super.p()) / / 3
console.log(super.pA)// undefined}}// The inner this after calling super points to an instance of the subclass
class A{
constructor(){
this.x = 1
}
print(){
console.log(this.x); }}class B extends A{
constructor(){
super(a)this.x = 2
}
fn(){
super.print()
// equivalent to super.print.call(this) in es5}}// assign the property by super, which equals this
class A{
constructor(){
this.x = 1}}// A.prototype.x = 3
class B extends A{
constructor(){
super(a)this.x = 2
super.x = 3 // this.x = 3
console.log(super.x); // undefined
console.log(this.x); / / 3}}// super as an object in a static method
// Point to the parent class instead of the prototype object
class Parent{
static myMethod(msg){
console.log(`static-${msg}`);
}
myMethod(msg){
console.log(` common -${msg}`); }}class Child extends Parent{
static myMethod(msg){
super.myMethod(msg)
}
myMethod(msg){
super.myMethod(msg)
}
}
// when a static method of a subclass calls a method of its parent class through super
// This inside the method refers to the current subclass instead of the instance of the subclass
class A{
constructor(){
this.x = 1
}
static print(){
console.log(this.x); }}class B extends A{
constructor(){
super(a)this.x = 2
}
static fn(){
super.print()
}
}
Copy the code