Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Class extends value #<Object> is not a constructor or null"

Tags:

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

like image 696
Charlote22 Avatar asked Apr 16 '18 10:04

Charlote22


People also ask

How do you extend classes?

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.

What is extending a class in JavaScript?

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.

How do you extend a function in JavaScript?

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

How do you inherit JavaScript?

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.


1 Answers

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.

like image 82
kigiri Avatar answered Oct 21 '22 21:10

kigiri