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(){
}
By default, each class in Node. js can extend only a single class. That means, to inherit from multiple classes, you'd need to create a hierarchy of classes that extend each other. If you're with NPM v4 or lower, just append a -S to the install command to automatically add it to the dependencies in package.
The extends keyword is used to create a child class of another class (parent). The child class inherits all the methods from another class. Inheritance is useful for code reusability: reuse properties and methods of an existing class when you create a new class.
The extends keyword can be used to subclass custom classes as well as built-in objects. Any constructor that can be called with new (which means it must have the prototype property) can be the candidate for the parent class. The prototype of the ParentClass must be an Object or null .
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With