Super方法

Super方法用来调用和访问父类的方法

super 关键词可以单独出现在子类的构造函数中,调用父类的构造函数为子类属性赋值(注意super关键词必须出现在this关键词之前)。

class Father{
    constructor(x, y){
        this.x = x;
        this.y = y;
        this.name = 'Father'
    }
    sayName(){
        console.log(this.name)
    }
}
class Child extends Father{
    constructor(x, y){
        super(x, y);
        this.name = 'Child';
    }
    sayPosition(){
        console.log(`x:${this.x},y:${this.y}`);
    }
}
new Child(5,10).sayPosition(); // x:5,y:10

super 方法还可以调用父类的函数

class Father{
    constructor(x, y){
        this.x = x;
        this.y = y;
        this.name = 'Father'
    }
    sayName(){
        console.log(this.name)
    }
}
class Child extends Father{
    constructor(x, y){
        super(x, y);
        this.name = 'Child';
    }
    sayName(){
        super.sayName();
    }
}
new Child().sayName(); // Child

super 只能访问父类的方法,不能直接访问属性,访问属性需要使用 super.constructor.Property

class Father{
    constructor(x, y){
        this.x = x;
        this.y = y;
        this.name = 'Father'
    }
}
class Child extends Father{
    constructor(x, y){
        super(x, y);
        this.name = 'Child';
    }
    test(){
        console.log(super.name,super.constructor.name); // 试试是否能访问父类的属性
    }
}
new Child().test(); // undefined,"Father"

我们知道在类中访问自身的静态方法分为两种

1.在一个静态方法中直接使用 this.staticMethod来访问另一个静态方法

2.在非静态方法中使用 CLASS.staticMethod或者是this.constructor.staticMethod来访问静态方法

class Father{
    constructor(x, y){
        this.x = x;
        this.y = y;
        this.name = 'Father'
    }
    static ping(){
        return 'ping';
    }
    static bing(){
        // 静态方法中直接使用 this.staticMethod 来访问
        console.log(`bing,${this.ping()}`);
    }
    unStatic(){
        // 非静态方法中使用 this.constructor.staticMethod 来访问
        console.log(this.constructor.ping());
    }
}
Father.bing(); // bing,ping
new Father().unStatic();// ping

在子类中访问父类的静态方法与此类似,

1.在子类静态方法中访问父类静态方法 super.FatherStaticMethod

2.在子类非静态方法中访问父类静态方法 super.constructor.FatherStaticMethod

当然,无论什么时候,使用Father.staticMethod来访问静态方法都是可以的。

class Father{
    constructor(x, y){
        this.x = x;
        this.y = y;
        this.name = 'Father'
    }
    static ping(){
        return 'ping';
    }
}
class Child extends Father{
    constructor(x, y){
        super(x, y);
        this.name = 'Child';
    }
    sayName(){
        // 在子类的非静态方法中访问父类静态方法
        console.log(super.constructor.ping());
    }
    static bing(){
        // 在子类的静态方法中访问父类静态方法
        console.log(`bing,${super.ping()}`);
    }
}
new Child().sayName(); // ping
Child.bing(); // bing,ping
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
ES6中,super方法用于在子类中调用父类的构造函数、静态方法或原型方法。它必须在使用this关键字之前调用,否则会抛出一个ReferenceError错误。 1. 调用父类构造函数 子类必须在构造函数中调用super方法来执行父类的构造函数,以便正确地设置子类的this对象。例如: ``` class Animal { constructor(name) { this.name = name; } } class Dog extends Animal { constructor(name, breed) { super(name); this.breed = breed; } } const myDog = new Dog('Buddy', 'Golden Retriever'); console.log(myDog.name); // 'Buddy' console.log(myDog.breed); // 'Golden Retriever' ``` 在这个例子中,Dog类继承自Animal类,因此它必须调用Animal类的构造函数来设置this.name属性。通过调用super(name),Dog类将传递给Animal构造函数的参数name。 2. 调用父类静态方法 子类可以调用父类的静态方法,通过使用super关键字。例如: ``` class Animal { static sayHi() { console.log('Hi from Animal'); } } class Dog extends Animal { static sayHi() { super.sayHi(); console.log('Hi from Dog'); } } Dog.sayHi(); // 'Hi from Animal' 'Hi from Dog' ``` 在这个例子中,Dog类继承自Animal类,并覆盖了Animal的sayHi静态方法。通过使用super关键字,Dog类调用了Animal的sayHi静态方法,并在后面添加了自己的输出。 3. 调用父类原型方法 子类可以调用父类的原型方法,通过使用super关键字。例如: ``` class Animal { sayHi() { console.log('Hi from Animal'); } } class Dog extends Animal { sayHi() { super.sayHi(); console.log('Hi from Dog'); } } const myDog = new Dog(); myDog.sayHi(); // 'Hi from Animal' 'Hi from Dog' ``` 在这个例子中,Dog类继承自Animal类,并覆盖了Animal的sayHi原型方法。通过使用super关键字,Dog类调用了Animal的sayHi方法,并在后面添加了自己的输出。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值