10

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(){
} 
Sara Fuerst
  • 5,688
  • 8
  • 43
  • 86
  • [`util.inherits`](https://nodejs.org/api/util.html#util_util_inherits_constructor_superconstructor) – Bergi Apr 19 '16 at 18:34
  • @Bergi Can you elaborate a bit? I would need to add `util`, then would get the desired functionality with `util.inherits(BlockMosaicStreamer, MosaicStreamer)`? – Sara Fuerst Apr 19 '16 at 18:57
  • Yes, and put a `MosaicStreamer.call(this, …)` "super" call in the `BlockMosaicStreamer` constructor. – Bergi Apr 19 '16 at 19:27
  • @Bergi I'm not familiar with how the "super" call would work. What would be replaced by the ...? – Sara Fuerst Apr 19 '16 at 19:40
  • Any arguments that you want to pass into the parent constructor (possibly none). It's just [`call`ing](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/call) the constructor on the new instance to get it initialised - no magic. – Bergi Apr 19 '16 at 19:45

1 Answers1

24

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
Nick Messing
  • 494
  • 5
  • 14