简体   繁体   English

如何在es6 javascript类中获取父函数?

[英]How to get parent function in es6 javascript classes?

If I have something like 如果我有类似的东西

class A {
    constructor() {
        this.test = function(){return 1;};
    }
}

class B extends A {
    constructor() {
        super(); 
        this.test = function(){alert(  super.test()   );};
    }
}

This does not work, but is there a way I can access the parent function test()? 这不起作用,但是有没有办法我可以访问父函数test()?

This does not work, but is there a way I can access the parent function test()? 这不起作用,但是有没有办法我可以访问父函数test()?

Because you're not using the prototype for the base class method, you can't use super.test() to reference it. 因为您没有将原型用于基类方法,所以不能使用super.test()来引用它。 Instead, this.test is just a property of your current object. 相反, this.test只是您当前对象的一个​​属性。 If there's some reason you don't want to use the prototype for methods, you could do this: 如果出于某些原因您不想使用原型作为方法,则可以执行以下操作:

class A {
    constructor() {
        this.test = function(){return 1;};
    }
}

class B extends A {
    constructor() {
        super();
        let priorTest = this.test; 
        this.test = function(){alert(  priorTest.call(this)   );};
    }
}

To further explain, there's only one this.test property so when you assign this.test = ... in class B, you overwrite the previous this.test so you can't then refer to it in the implementation. 为了进一步说明,只有一个this.test属性,因此当您在类B中分配this.test = ...时,您将覆盖以前的this.test因此您将无法在实现中引用它。 But, you can save its value before you overwrite it and then use that. 但是,您可以先保存它的值,然后再覆盖它,然后使用它。

Also, some people mistakenly think that this refers to a different object inside a base class method vs. a derived class method. 同样,有些人错误地认为this是指基类方法与派生类方法内部的不同对象。 That is not the case. 事实并非如此。 There's only one object and both the base class and derived class have methods on that object. 只有一个对象,并且基类和派生类都对该对象具有方法。 So, this.test = xxx in the base class refers to the exact same property as this.test = yyy in the derived class. 因此,基类中的this.test = xxx与派生类中的this.test = yyy完全相同。 Methods defined on the prototype are actually saved on separate objects though so they can all exist independently. 尽管原型上定义的方法实际上保存在单独的对象上,所以它们都可以独立存在。 When you reference this.test and there is no "own" property directly on the current object, then Javascript will search the prototype chain to see if it finds a property in the prototype chain with the desired name. 当您引用this.test并且当前对象上没有直接的“拥有”属性时,Javascript将搜索原型链以查看是否在原型链中找到具有所需名称的属性。 If it does, then it will return/execute that one. 如果是这样,它将返回/执行该命令。


The ES6 way to do this is to use the ES6 syntax for defining methods which will use the prototype and then you can use super.test() to refer to the base class implementation because in the prototype chain each class gets its own object on which to define methods that are searched if there is no "own" property with the specified name. ES6的方法是使用ES6语法定义将使用原型的方法,然后可以使用super.test()来引用基类的实现,因为在原型链中,每个类都有自己的对象,定义没有指定名称的“ own”属性时要搜索的方法。

class A {
    constructor() {
    }
    test() {
        return 1;
    }
}

class B extends A {
    constructor() {
        super(); 
    }
    test() {
        alert(super.test());
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM