Home > front end >  How to implement a class creation pattern where one class would conditionally extend any other given
How to implement a class creation pattern where one class would conditionally extend any other given

Time:11-03

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

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; }

  • Related