Straightforward Way to Extend Class in Node.js

Sara Tibbetts picture Sara Tibbetts · Apr 19, 2016 · Viewed 17.1k times · Source

I am moving a plain Javascript class into Node.js. In the plain Javascript I use:

class BlockMosaicStreamer extends MosaicStreamer{
}

I can't seem to find a simple way to implement this in Node.js. In my node project in BlockMosaicStreamer.js I have:

'use strict'; 
function BlockMosaicStreamer(){
} 

How would I extend MosaicStreamer which is in ./MosaicStreamer.js?

'use strict'; 
function MosaicStreamer(){
} 

Answer

Nick Messing picture Nick Messing · Apr 19, 2016

It depends how you defined your first class, I suggest using something like this:

class SomeClass {
}

module.exports = SomeClass

then in your extend:

const SomeClass = require('./dir/file.js')

class MyNewClass extends SomeClass {
}

module.exports = MyNewClass