javascript “类扩展值 #<Object> 不是构造函数或 null”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/49854811/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 08:48:04  来源:igfitidea点击:

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

javascriptes6-class

提问by Charlote22

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.

感谢阅读我的帖子,我的代码出现此错误:“Class extends value # is not a constructor or null”这是我的代码,我正在尝试导出/导入类。

monster.js :

怪物.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 :

小怪物.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

祝你有美好的一天

采纳答案by kigiri

I see at least one issue with your requires.

我发现您的需求至少有一个问题。

  • monster.jsfirst line is const miniMonster = require("./minimonster.js");
  • minimonster.jsfirst line is const monster = require("./monster.js");
  • monster.js第一行是 const miniMonster = require("./minimonster.js");
  • minimonster.js第一行是 const monster = require("./monster.js");

This is a problem, you can not have both files evaluate at the same time. I would not require minimonsterfrom monster.js

这是一个问题,您不能同时评估两个文件。我不会要求minimonstermonster.js

This may fix your issue.

这可能会解决您的问题。

回答by Dave Pile

When I get this error message it is because I have done my module.exportswrong. eg.

当我收到此错误消息时,是因为我module.exports做错了。例如。

publicclass.js

公共类.js

class PublicClass {
.....
}

module.exports.PublicClass = PublicClass;

instead of

代替

module.exports = PublicClass;

回答by lwdthe1

For me, I had introduced a circular dependency.

对我来说,我引入了循环依赖。

回答by Rahul Mogar

I used as per below line.

我按照下面的行使用。

import Base from "./base

and its working fine now

现在它工作正常