1

I am writing a package and the base class which a user usually uses to access the package functions doesn’t have any members just accepts a parameter in the constructor and is acting as a proxy for other classes with members. So the base class will act like a funnel for other classes and their members to pass on one class at a time when user wants to access it.

Now there can be a number of classes other than the base class and when a user passes the name of a particular class can somehow get to access members of that class.

There can only be one class proxied at a time. 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

Edit: I was wondering if it’s possible to pass the constructor parameters from the base class to the class it will extend.

  • 1
    You should probably just use a factory function. – kelsny Nov 02 '22 at 13:46
  • I think we need to see more of a use case before we can suggest how to proceed. Does `C` have its own methods or is it just a proxy to either `A` or `B`? You could do [this](https://tsplay.dev/mArzvW) but it's pretty nasty, so I want to understand the use case. If you [edit] with more info and want me to take another look, please reply with a comment mentioning @jcalz, so I'm notified. Good luck! – jcalz Nov 02 '22 at 15:29
  • @jcalz made some edits, let me know if it makes sense. – Mehul Chaturvedi Nov 03 '22 at 04:59

1 Answers1

1

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; }
Peter Seliger
  • 11,747
  • 3
  • 28
  • 37
  • Thanks, Peter, inspired by your, using mixin, played around with types for a bit and I was able to accomplish my goal. – Mehul Chaturvedi Nov 03 '22 at 08:14
  • 1
    @MehulChaturvedi ... you're welcome ... and ... _**the above approach is NOT about using mixins**_; one might name it _"dynamic sub-classing"_ or _"dynamic sub-typing"_. – Peter Seliger Nov 03 '22 at 08:18