Skip to content

TypeError: m is not a function, when extending a class that uses mixins #27

Open
@aedart

Description

It appears that it is not possible to extend a class, which is making use of the mix().

The following illustrates the point (at least when bundling with Webpack, without transpiling)

import { mix } from 'mixwith/src/mixwith';

// A mixin of sorts...
let MyMixin = (superclass) => class extends superclass {

    constructor(){
        super();
        console.log('Mixin');
    }
};

// A class that uses that mixin
class A extends mix(Object).with(MyMixin) {

    constructor(){
        super();
        console.log('Class A');
    }
}

// ...Later, a class that inherits from A
class B extends A {
    constructor(){
        super();
        console.log('Class B');
    }
}

// This fails, ...
let classB = new B();

Location of defect

The issue lies within the MixinBuilder class, in the with method, in that if the superclass is undefined or null, then the method does not handle such exception.

class MixinBuilder {

  constructor(superclass) {
    this.superclass = superclass;
  }

  with() {
    // Here - m might be undefined!
    return Array.from(arguments).reduce((c, m) => m(c), this.superclass);
  }
}

Possible Solution

When editing in the published / bundled source file, I got the above mini-test to work as intended.

class MixinBuilder {

  constructor(superclass) {
    this.superclass = superclass;
  }

  with() {

    return Array.from(arguments).reduce((c, m) => {
      if(typeof m !== "function"){
        return c;
      }

      return m(c);
    }, this.superclass);
  }
}

Please patch as soon as possible

This defect is currently blocking me, because I need to extend a class that I'm not in control of (external dependency), without the usage of mix(). Can you please release a path to this issue, as soon as possible?

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions