Repository navigation
util.inherits cannot be used with ES6 classes #3452
Description
Activity
- addedutilIssues and PRs related to the built-in util module.Issues and PRs related to the built-in util module.feature requestIssues requesting new Node.js features.Issues requesting new Node.js features.
on Oct 20, 2015 I think
util.inherits()would need to start usingObject.setPrototypeOf()instead of assigning to the.prototypeproperty directly? Not sure how it should interact with the.constructorproperty 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.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 asclass X {} class Y extends X {} util.inherits(Y, EventEmitter);
This is not possible.
util.inheritsoverwrites the prototype chain of Y, removing the inheritance on X.Reacted by Bergi and Thiago LucioThat makes sense. Thanks.
- added a commit that references this issue
on Oct 29, 2015 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.
@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 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);
…fails to evaluate, resulting in:
> class Child { ... constructor () { ..... super(); super(); ^^^^^ SyntaxError: 'super' keyword unexpected hereNew issue, or related?
Reacted by Adam Charvát and Chris Watsonsuper()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);
…fails to evaluate, resulting in:
> class Child { ... constructor () { ..... super(); super(); ^^^^^ SyntaxError: 'super' keyword unexpected hereNew issue, or related?
@targos Could you please clarify why
superisn't available when usingutil.inherits
Error:SyntaxError: 'super' keyword unexpected ...Apparently using
Child.super_works, but could there be a similar way that works withsuperfunction implementation ?Reacted by David Kebler, TenviLi and Adam Charvátdoes this question solved?
how to handle the keywordsuper
I'm experimenting with ES6 classes and noticed I was unable to use
util.inheritsto inheritEventEmitterinto the prototype of my class.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 usingextendsmeans my class can't be a subclass of anything besidesEventEmitterunless I makeEventEmitterthe root class.Thoughts on this?