简体   繁体   English

ES6使用`super`出类定义

[英]ES6 use `super` out of class definition

I'm trying to add extra methods to class, and these extra methods should use the super methods. 我正在尝试向类添加额外的方法,这些额外的方法应该使用super方法。

If I add them in the model definition, it works. 如果我在模型定义中添加它们,它就可以工作。

class A {
    doSomething() {
        console.log('logSomething');
    }

}

class B extends A {
    doSomething() {
        super.doSomething();
        console.log('logSomethingElse');
    }
}

If I try to add the extra method to B.prototype , I'll get SyntaxError: 'super' keyword unexpected here . 如果我尝试将额外的方法添加到B.prototype ,我会SyntaxError: 'super' keyword unexpected here得到SyntaxError: 'super' keyword unexpected here

class A {
    doSomething() {
        console.log('logSomething');
    }

}

class B extends A {
}

B.prototype.doSomething = function doSomething() {
    super.doSomething();
    console.log('logSomethingElse');
}

It is quite clear, why I get this error. 很明显,为什么我会收到这个错误。 This is a function and not a class method. 这是一个函数而不是类方法。

Let's try to define the method as a class method, and copy it to the original B class: 让我们尝试将方法定义为类方法,并将其复制到原始B类:

class A {
    doSomething() {
        console.log('logSomething');
    }

}

class B extends A {}

class X {
    doSomething() {
        super.doSomething();
        console.log('2 logSomethingElse');
    }
}

B.prototype.doSomething = X.prototype.doSomething;

In this case I'll get TypeError: (intermediate value).doSomething is not a function . 在这种情况下,我将得到TypeError: (intermediate value).doSomething is not a function

Is there any way to define methods (that refer to super ) outside from the original class definition, and add these methods later to the original class? 有没有办法在原始类定义之外定义方法(引用super ),然后将这些方法添加到原始类中?

super refers to ancestor of a class where the method was defined, it isn't dynamic. super指的是定义方法的类的祖先,它不是动态的。 As Babel output illustrates this, super is hard-coded to Object.getPrototypeOf(X.prototype) , and thus orphan class like this one doesn't make sense because it doesn't have super : 正如Babel输出说明的那样, super被硬编码为Object.getPrototypeOf(X.prototype) ,因此像这样的孤立类没有意义,因为它没有super

class X {
    doSomething() {
        super.doSomething();
        ...
    }
}

But super can be substituted with dynamic counterpart: super可以用动态对应物代替:

doSomething() {
    const dynamicSuper = Object.getPrototypeOf(this.constructor.prototype);
    // or
    // const dynamicSuper = Object.getPrototypeOf(Object.getPrototypeOf(this));
    dynamicSuper.doSomething();
    ...
}

class B extends A {}
B.prototype.doSomething = doSomething;

In this case it will refer to ancestor class of class instance where doSomething was assigned as prototype method. 在这种情况下,它将引用类实例的祖先类,其中doSomething被指定为原型方法。

While I think this could be assumed as anti-pattern , you shouldn't use super outside from a class . 虽然我认为这可以假设为反模式 ,但你不应该在class使用super外部。

You can achieve that using Object Literals . 您可以使用Object Literals实现此目的。

Refer to Object.setPrototypeOf 请参阅Object.setPrototypeOf

 const A = { sayHello() { console.log("I am A"); }, Factory() { return Object.create(this); } } const B = { sayHello() { super.sayHello(); } } Object.setPrototypeOf(B, A); const c = B.Factory(); c.sayHello(); 

如果你没有让类X继承自B类或A类,那么调用该方法的唯一方法是A.prototype.doSomething()或更普遍的A.prototype.doSomething.call(this_substitute, ...args)

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

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