Thanks for reading my post I get this error on my code : "Class extends value # is not a constructor or null" Here is my code, I'm trying to export/import classes.
monster.js :
const miniMonster = require("./minimonster.js"); class monster { constructor(options = { name }, health) { this.options = options; this.health = 100; this.heal = () => { return (this.health += 10); }; } } let bigMonster = new monster("Godzilla"); console.log(bigMonster); console.log(bigMonster.heal()); let mini = new miniMonster("Demon"); console.log(mini); console.log(mini.heal()); module.exports = monster;
minimonster.js :
const monster = require("./monster.js"); class miniMonster extends monster { constructor(options) { super(options); this.health = 50; this.heal = () => { return (this.health += 5); }; } } let miniM = new miniMonster("Jon"); console.log(miniM); module.exports = miniMonster;
Thank you for any help given,
Have a good day
Definition and UsageThe extends keyword extends a class (indicates that a class is inherited from another class). In Java, it is possible to inherit attributes and methods from one class to another. We group the "inheritance concept" into two categories: subclass (child) - the class that inherits from another class.
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.
Use extendFunction. init = extendFunction(init, function(args) { doSomethingHereToo(); }); But in your specific case, it's easier to extend the global onload function: extendFunction('onload', function(args) { doSomethingHereToo(); });
When it comes to inheritance, JavaScript only has one construct: objects. Each object has a private property which holds a link to another object called its prototype. That prototype object has a prototype of its own, and so on until an object is reached with null as its prototype.
I see at least one issue with your requires.
monster.js
first line is const miniMonster = require("./minimonster.js");
minimonster.js
first line is const monster = require("./monster.js");
This is a problem, you can not have both files evaluate at the same time. I would not require minimonster
from monster.js
This may fix your issue.
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