-
Notifications
You must be signed in to change notification settings - Fork 29.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
util.inherits cannot be used with ES6 classes #3452
Comments
I think |
@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 class X {}
class Y extends X {}
util.inherits(Y, EventEmitter); This is not possible. |
That makes sense. Thanks. |
The current implementation overwrites the prototype of the target constructor. It is not allowed with ES2015 classes because the prototype property is read only. Use Object.setPrototypeOf instead. Fixes: nodejs#3452 PR-URL: nodejs#3455 Reviewed-By: Sakthipriyan Vairamani <[email protected]> Reviewed-By: James M Snell <[email protected]>
The current implementation overwrites the prototype of the target constructor. It is not allowed with ES2015 classes because the prototype property is read only. Use Object.setPrototypeOf instead. Fixes: nodejs#3452 PR-URL: nodejs#3455 Reviewed-By: Sakthipriyan Vairamani <[email protected]> Reviewed-By: James M Snell <[email protected]>
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 ;-) |
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:
New issue, or related? |
@targos Could you please clarify why Apparently using |
does this question solved? |
I'm experimenting with ES6 classes and noticed I was unable to use
util.inherits
to inheritEventEmitter
into 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 usingextends
means my class can't be a subclass of anything besidesEventEmitter
unless I makeEventEmitter
the root class.Thoughts on this?
The text was updated successfully, but these errors were encountered: