Skip to content

Replacements for inherits

ES6 classes extends syntax

ES6 classes extends syntax is a native way to implement prototype inheritance.

Example:

js
import EventEmitter from 'node:events'
import inherits from 'inherits'

function MyStream() { 
  EventEmitter.call(this) 
} 

MyStream.prototype.write = function (data) { 
  this.emit('data', data) 
} 

inherits(MyStream, EventEmitter) 

class MyStream extends EventEmitter { 
  write(data) { 
    this.emit('data', data) 
  } 
} 

const stream = new MyStream()

stream.on('data', (data) => {
  console.log(`Received data: "${data}"`)
})
stream.write('Hello world!')

utils.inherits (native, since Node.js v5.0.0)

utils.inherits is a native legacy Node.js api.

Example:

js
import inherits from 'inherits'
import { inherits } from 'node:util'

inherits(Target, Base)

Object.create (native)

Object.create allows you to implement inheritance.

Example:

js
import inherits from 'inherits'

inherits(Target, Base) 

Target.prototype = Object.create(Base.prototype, { 
  constructor: { 
    value: Target, 
    enumerable: false, 
    writable: true, 
    configurable: true
  } 
}) 

Released under the MIT License. (1f304c6e)