I currently have two classes, each with a child class that shares the same extended functions. I am looking for a way to avoid code duplication by creating a more loosely connected class.
The main reason for extending the parent class is to access its methods within the child class.
See code below.
class Base1 {
/**
* @param {() => cy} getWrapper
*/
constructor( getWrapper ) {
this._getWrapper = getWrapper;
}
getSomething () {
return this.getWrapper().find('something');
}
getWrapper () {
return this._getWrapper();
}
}
class Base2 {
/**
* @param {() => cy} getWrapper
*/
constructor( getWrapper ) {
this._getWrapper = getWrapper;
}
getHeading () {
return this.getWrapper().find('heading');
}
getWrapper () {
return this._getWrapper();
}
}
class ReWrapBase1 extends Base1 {
eq ( index ) {
return new Base1( () => this.getWrapper().eq( index ) );
}
getWrapper () {
return this._getWrapper().find( '[class*=SummaryItemstyles__Wrapper]' );
}
}
class ReWrapBase2 extends Base2 {
eq ( index ) {
return new Base2( () => this.getWrapper().eq( index ) );
}
getWrapper () {
return this._getWrapper().find( '[class*=SummaryItemstyles__Wrapper]' );
}
}
At first, I thought of having the constructor store the class, but then realized I couldn't access the parent's methods, which defeats the purpose.
class ReWrapAny {
constructor(returnedClass) {
this._returnedClass = returnedClass
}
eq ( index ) {
return new this._returnedClass( () => this.getWrapper().eq( index ) );
}
getWrapper () {
return this._getWrapper().find( '[class*=SummaryItemstyles__Wrapper]' );
}
}