3

Is it possible to create a class that instantiates functions with methods on it's prototype? I am trying to convert code from prototype structure to using the es6 class syntax. Here is a contrived and over simplified example of the starting point

function createFun(init) {
  function fun(newDats) {
    this.data = newDats;
    // create universe
  }
  function internalMethod() {
  }
  fun.data = init;
  fun.aMethod = function () {
    internalMethod();
  }
  assign(fun, AnExtendableClass.prototype);
  return fun;
}

// and can be used as such
fun = createFun('first');
fun('second');
fun.aMethod();
fun.methodFromExtendableClass('third')

And this is what I have tried

class Fun extend AnExtendableClass {
   constructor(init) {
     super();
     this.data = init;
     function fun(newDats) {
       this.data = newDatas;
       //create universe
     }
     assign(fun, this);
     return fun;
   }

   aMethod() {
   }
}

Unfortunately this does not work and function with no methods in return.

5
  • What's the point of using a class for creating something that is not an instance of itself? Having a function that produces the class seems to be much more reasonable. It makes sense to turn Fun into a class. It doesn't make sense to turn createFun into a class. Commented Mar 25, 2015 at 5:27
  • @FelixKling Turning Fun into a class is what I am trying to accomplish. createFun is just a class instantiator. I have other create functions that I am converting to straight classes successfully but this one class is a function object with methods and that is giving me issues. Commented Mar 25, 2015 at 14:50
  • @JLRishe assign is equivalent to Object.assign or _.assign Commented Mar 25, 2015 at 14:50
  • I see. I didn't notice first that you where merging OtherConstr.prototype into Fun. Just keep that line, see my answer. Commented Mar 25, 2015 at 15:01
  • I guess it comes back to my first comment again. It seems you actually want to replace createFunc with a class, not Fun. As I said, that doesn't make sense. Judging from the use case, nothing in this example should (or can be) converted to classes. Commented Mar 25, 2015 at 15:51

1 Answer 1

1

Is it possible to create a class that instantiates functions with methods on it's prototype?

Yes, with ES6 it is possible to subclass Function - however, that's not exactly nice, as the constructor expects code strings:

class Fun {
    constructor() {
        super("newDats", `
            this.data = newDats;
            // create universe
        `)
    }
    data() { }
    aMethod() { }
}

let fun = new Fun;
fun(…);
fun.aMethod(…);

I am trying to convert code from prototype structure to using the es6 class syntax. Here is a contrived and over simplified example of the starting point

Don't use the class syntax for that. The new syntax is very limited, and should only be used for standard class declarations. If you do anything weird - and returning functions from the constructor, copying methods from other classes, assigning static properties and using internal methods is definitely weird in this regard - then go use the "old", explicit way. The new Reflect.setPrototypeOf is giving you additional freedom here.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks @Bergi I guess it makes more sense to leave this bit of code as it is. Being able to create functions from classes seems like something that should be do-able in javascript but I guess that is not within the intention of this syntax.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.