I'm experimenting with ES6 classes and noticed I was unable to use util.inherits to inherit EventEmitter into the prototype of my class.
'use strict';
var util = require('util');
var EventEmitter = require('events').EventEmitter;
class x {
constructor() {
EventEmitter.call(this);
}
emitEvent() {
this.emit('event emitted!');
}
}
util.inherits(x, EventEmitter); // Fails
The error is TypeError: Cannot assign to read only property 'prototype' of
class x {
constructor() {
EventEmitter.call(this);
}
emitEvent() {
this.emit('event emitted!');
}
}
I was able to get inheritance to work using class x extends EventEmitter, but using extends means my class can't be a subclass of anything besides EventEmitter unless I make EventEmitter the root class.
Thoughts on this?
I think util.inherits() would need to start using Object.setPrototypeOf() instead of assigning to the .prototype property directly? Not sure how it should interact with the .constructor property it tries to set.
@bnoordhuis I just tried your suggestion and it looks good. I'll add a test and PR it if you don't mind.
@sbmelvin to be clear, #3455 just allows to use util.inherits on ES6 classes (and cleans a little bit the implementation). The result will be similar to what you get with extends.
It seems from your post that you would like to do multiple inheritance, such as
class X {}
class Y extends X {}
util.inherits(Y, EventEmitter);
This is not possible. util.inherits overwrites the prototype chain of Y, removing the inheritance on X.
That makes sense. Thanks.
Just wanted to note that this is not fixed in LTS, but Stable only. Shouldn't this be fixed in LTS, also? This isn't adding new features, this is fixing a bug, and as far as I know, class is supported in LTS, too.
@shelleyp it looks like that PR was labelled Semver Major... I doubt that it would be backported.
/cc @jasnell @nodejs/lts
@TheAlphaNerd that's cool, I wasn't sure if this is one that should be backported or not. I'm just going to assume any ES6 issues, regardless of origin, won't be in LTS, so don't clutter up issues ;-)
super() does not seem to be available in the constructor when using util.inherits on an ES6 class:
const util = require('util');
class Parent {
constructor () {
console.log('Hello from Parent!');
}
}
class Child {
constructor () {
super();
}
}
util.inherits(Child, Parent);
鈥ails to evaluate, resulting in:
> class Child {
... constructor () {
..... super();
super();
^^^^^
SyntaxError: 'super' keyword unexpected here
New issue, or related?
super()does not seem to be available in the constructor when usingutil.inheritson an ES6 class:const util = require('util'); class Parent { constructor () { console.log('Hello from Parent!'); } } class Child { constructor () { super(); } } util.inherits(Child, Parent);鈥ails to evaluate, resulting in:
> class Child { ... constructor () { ..... super(); super(); ^^^^^ SyntaxError: 'super' keyword unexpected hereNew issue, or related?
@targos Could you please clarify why super isn't available when using util.inherits
Error: SyntaxError: 'super' keyword unexpected ...
Apparently using Child.super_ works, but could there be a similar way that works with super function implementation ?
Most helpful comment
@sbmelvin to be clear, #3455 just allows to use
util.inheritson ES6 classes (and cleans a little bit the implementation). The result will be similar to what you get withextends.It seems from your post that you would like to do multiple inheritance, such as
This is not possible.
util.inheritsoverwrites the prototype chain of Y, removing the inheritance on X.