0

I like the old non-class way of declaring classes in Javascript, because I haven't quite figured out how to use this properly in classes.

function _foo(){
    this.bar=10;
}
var foo = new _foo();

But I also want to be able to let _foo extend another class, say EventEmitter. How would I go about doing this?

I've tried the following:

function _foo(){
   EventEmitter.call(this);
}
var foo=new _foo();
foo.on(); //throws error: foo.on is not a function - even though EventEmitter.on is a function.

Thanks

acenturyandabit
  • 1,188
  • 10
  • 24
  • "because I haven't quite figured out how to use this properly in classes" — `class` syntax is **much** simpler … and `this` is used in the same way as old-style constructor function + prototype syntax. It would be really worth you figuring that out. – Quentin Aug 01 '19 at 12:28
  • I'm currently adding a `let me=this` at the top of my function constructor so that I can access `this` as the instance in any function called by e.g. an addEventListener call without `this` ending up as `window`. This works for `async` functions and bracket functions. Am I horrifically abusing Javascript? Should I care? – acenturyandabit Aug 02 '19 at 00:16
  • * is there a way to put `me=this` in a class declaration? – acenturyandabit Aug 02 '19 at 00:26

0 Answers0