简体   繁体   中英

ES6: access to inherited class'es properties and methods from the parent class

ES6 has not abstract methods or properties, but can I get some methods or properties in the parent class from inherited class?

class ParentClass {

    constructor(){

      ParentClass.checkClildPropertyAccessibility();
      ParentClass.checkClildMethodAccessibility();

      ParentClass.checkClildStaticPropertyAccessibility();
      ParentClass.checkClildStaticMethodAccessibility();

    }

    static checkClildPropertyAccessibility() {
        console.log(ParentClass.childProperty);
    }

    static checkClildMethodAccessibility(){
        ParentClass.childMethod()
    }

    static checkClildStaticPropertyAccessibility(){
        console.log(ParentClass.childStaticProperty);
    }

    static checkClildStaticMethodAccessibility(){
        ParentClass.clildStaticMethod()
    }
}

class ChildClass extends ParentClass {

    constructor(){
        super();
        ChildClass.childProperty = 'child\'s Property: OK';
    }

    childMethod(){
        console.log('child\'s method OK');
    }

    // static property emulation is ES6
    static get childStaticProperty() { return 'Child\'s static property: OK even ES6' }

    static clildStaticMethod (){
        console.log('Child\'s static method: OK');
    }      
}

let childClassInstance = new ChildClass(); 

The concept is "We must to define some properties and methods in child class, however the parent class needs them to use already in constructor".

It is possible to refer current constructor in parent class as this in static methods and as this.constructor in instance methods.

This results in potential design problem, because ParentClass doesn't have methods and properties that are defined in ChildClass , and new ParentClass will result in error when non-existent childMethod is called.

Yes , it's possible to call methods only defined in a subclass of an ES2015 class.

 class ParentClass { constructor() { this.childMethod(); this.initialize(); console.log(this.childProperty1); console.log(this.childProperty2); } } class ChildClass extends ParentClass { constructor() { super(); this.childProperty1 = 'child\\'s Property: OK'; } initialize() { this.childProperty2 = 'child\\'s Property 2: OK'; } childMethod() { console.log('Child\\'s overriden method: OK'); } } let childClassInstance = new ChildClass(); 

Notice initialize() is used to assign an initial value to childProperty2 . Parent constructor will always run before any other code in a subclass constructor, that's why childProperty1 isn't initialized when the console call was made. In a parent class, this will point to an object with the prototype of the subclass. In the comments section, Bergi points out the practice of calling an overridden method in a parent constructor should be avoided, given the overriden method may depend on state not yet setup by the child constructor.

And no , it doesn't work with static methods. Even though static methods are copied to subclasses, when you refer to ParentClass , you'll get the method defined there. There's no prototype chain to follow there.

EDIT : clarification from comments section.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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