javascriptnode.jsoopextending

Straightforward Way to Extend Class in Node.js


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(){
} 

Solution

  • 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