Skip to content

util.inherits cannot be used with ES6 classes #3452

Description

@zen-crab

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?

Activity

  1. added
    utilIssues and PRs related to the built-in util module.
    feature requestIssues requesting new Node.js features.
    on Oct 20, 2015
  2. bnoordhuis commented on Oct 20, 2015

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

  3. targos commented on Oct 20, 2015

    @targos
    Member

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

  4. targos commented on Oct 20, 2015

    @targos
    Member
  5. targos commented on Oct 23, 2015

    @targos
    Member

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

  6. zen-crab commented on Oct 23, 2015

    @zen-crab
    Author

    That makes sense. Thanks.

  7. added a commit that references this issue on Oct 29, 2015
  8. shelleyp commented on Feb 9, 2016

    @shelleyp

    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.

  9. MylesBorins commented on Feb 9, 2016

    @MylesBorins
    Contributor

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

    /cc @jasnell @nodejs/lts

  10. shelleyp commented on Feb 9, 2016

    @shelleyp

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

  11. martindale commented on Sep 4, 2018

    @martindale

    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?

  12. safizn commented on Mar 4, 2019

    @safizn

    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 ?

  13. TenviLi commented on Dec 4, 2020

    @TenviLi

    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

Metadata

Metadata

Assignees

No one assigned

    Labels

    feature requestIssues requesting new Node.js features.utilIssues and PRs related to the built-in util module.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions