Home > Mobile >  Typescript how to inherit another class conditionally inside constructor
Typescript how to inherit another class conditionally inside constructor

Time:11-03

I am writing a package and in the base class, I was wondering if there is a way to inherit a class conditionally and provide its members when the parent class's instance is created. For example

class A {
  classAFunction() {
    console.log('Class A')
  }
}

class B {
  classBFunction() {
    console.log('Class B')
  }
}

class C {
  constructor(classToInherit: string) {
    if(classToInherit === 'A') {
      // inherit class A and provide its functions
    } else {
      // inherit class B and provide its functions
    }
  }
}

Inherit in such a way that when class C is instantiated all the inherited members are available. For example

const classC = new C('A')
classC.classAFunction()

I am not sure if it's the right way of doing as I have shown in the examples but the basic idea is to bind other class members with the current class in the constructor so they are accessible when creating instance of class C in the example.

Thank you very much

CodePudding user response:

The following provided factory-pattern, which creates an ad-hoc implementation of an anonymous extended class, commonly gets referred to as "es-6 class mixin" or "mixin-class". The factory-function itself is mostly named Mixin.

Regarding the language's very core features all of this of cause represents not the best wording for it shows a lack of understanding the concept of mixins.

The example code provides a possible solution to the OP's problem, and it proves that any class-factory based on a class expression and extends deals with just inheritance.

class TypeA {
  prototypalAMethod() {
    console.log('prototypal A');
  }
}
class TypeB {
  prototypalBMethod() {
    console.log('prototypal B');
  }
}

// factory which creates an anonymous extended class.
const createFuzzyTypeCustomClass = superclass => class extends superclass {
  prototypalCustomMethod() {
    console.log('prototypal C(ustom)');
  }
};

const fuzzyCustomAType = new (createFuzzyTypeCustomClass(TypeA));
const fuzzyCustomBType = new (createFuzzyTypeCustomClass(TypeB));

console.log(
  '(fuzzyCustomAType instanceof TypeA) ?..',
  (fuzzyCustomAType instanceof TypeA)
);
console.log(
  '(fuzzyCustomAType instanceof TypeB) ?..',
  (fuzzyCustomAType instanceof TypeB)
);
fuzzyCustomAType.prototypalCustomMethod();
fuzzyCustomAType.prototypalBMethod?.();
fuzzyCustomAType.prototypalAMethod();

console.log(
  '(fuzzyCustomBType instanceof TypeA) ?..',
  (fuzzyCustomBType instanceof TypeA)
);
console.log(
  '(fuzzyCustomBType instanceof TypeB) ?..',
  (fuzzyCustomBType instanceof TypeB)
);
fuzzyCustomBType.prototypalCustomMethod();
fuzzyCustomBType.prototypalBMethod();
fuzzyCustomBType.prototypalAMethod?.();
.as-console-wrapper { min-height: 100%!important; top: 0; }

CodePudding user response:

This is not my answer, I got it from a friend.

Basically, JavaScript is a hot mess, therefore something like this can be done with functions, but I couldn't make it work for classes.

function extendObj(base, sub){
  sub.prototype = Object.create(base.prototype);
  sub.prototype.consturctor = sub;
  sub.base = base.prototype;
  return sub;
}

function A(){}
function B(){}
extendObj(A,B);
var aObj = new A();
var bObj = new B();
console.log(bObj instanceof A); // true
console.log(bObj instanceof B); // true
  • Related