Skip to content
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

Closed
sbmelvin opened this issue Oct 20, 2015 · 11 comments
Closed

util.inherits cannot be used with ES6 classes #3452

sbmelvin opened this issue Oct 20, 2015 · 11 comments
Labels
feature request Issues that request new features to be added to Node.js. util Issues and PRs related to the built-in util module.

Comments

@sbmelvin
Copy link

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?

@Fishrock123 Fishrock123 added util Issues and PRs related to the built-in util module. feature request Issues that request new features to be added to Node.js. labels Oct 20, 2015
@bnoordhuis
Copy link
Member

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.

@targos
Copy link
Member

targos commented Oct 20, 2015

@bnoordhuis I just tried your suggestion and it looks good. I'll add a test and PR it if you don't mind.

@targos
Copy link
Member

targos commented Oct 20, 2015

#3455

@targos
Copy link
Member

targos commented Oct 23, 2015

@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.

@sbmelvin
Copy link
Author

That makes sense. Thanks.

targos added a commit to targos/node that referenced this issue Oct 27, 2015
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]>
rvagg pushed a commit to rvagg/io.js that referenced this issue Oct 29, 2015
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]>
@shelleyp
Copy link

shelleyp commented Feb 9, 2016

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.

@MylesBorins
Copy link
Contributor

@shelleyp it looks like that PR was labelled Semver Major... I doubt that it would be backported.

/cc @jasnell @nodejs/lts

@shelleyp
Copy link

shelleyp commented Feb 9, 2016

@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 ;-)

@martindale
Copy link

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);

…fails to evaluate, resulting in:

> class Child {
...   constructor () {
.....     super();
    super();
    ^^^^^

SyntaxError: 'super' keyword unexpected here

New issue, or related?

@safizn
Copy link

safizn commented Mar 4, 2019

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);

…fails to evaluate, resulting in:

> class Child {
...   constructor () {
.....     super();
    super();
    ^^^^^

SyntaxError: 'super' keyword unexpected here

New 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 ?

@TenviLi
Copy link

TenviLi commented Dec 4, 2020

does this question solved?
how to handle the keyword super

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request Issues that request new features to be added to Node.js. util Issues and PRs related to the built-in util module.
Projects
None yet
Development

No branches or pull requests

9 participants