1

I'm trying to create a class that extends Function.

var MyClass = class MyClass extends Function {
  constructor(func, /*some other things*/) {
    //this is not defined in extended classes for some reason
    var newThis = func;
    //assign other stuff to newThis
    return newThis;
  }

  //methods

}

At first I thought this would work but an instanceof check revealed that the object created was just a regular function without any of my methods or properties. So then I realised that I need to use the super keyword to construct a Function.

var newThis = super(func.toString().split('(')[1].split(')')[0].split(','),
                    func.toString().split('{')[1].split('}')[0])

This works but it doesn't comply with the content security policy (something like that) which means that it won't work in chrome apps.

Sneaky Turtle
  • 191
  • 1
  • 11

1 Answers1

0

I'm not 100% what you're trying to do, but in order to correctly extend a class in ES6 you must called super() before you do anything.

class Foo extends Function {

  constructor(){
    super();
    this.x = 'foo';
  }

}

let test= new Foo();
console.log(test.x); // 'foo'

You can try it out on the babel REPL here

Joe Ruello
  • 38
  • 2
  • I'm missing something here. `test` is a function that is defined how, and will do what when you call it? –  Jan 12 '16 at 05:19
  • I've never actually extended Function so I don't actually know. I can tell you that Function is a subclass of Object, and has a bunch of methods and properties on it's prototype for more details. See [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function) – Joe Ruello Jan 13 '16 at 06:26