Skip to content

util.inherits cannot be used with ES6 classes #3452

@zen-crab

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    feature requestIssues that request new features to be added to Node.js.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