Javascript 节点JS实例化类B中的类A

Javascript 节点JS实例化类B中的类A,javascript,node.js,Javascript,Node.js,我从node.js开始,我只想在我的类A中使用我的类B,但它似乎不起作用。。。这是我非常简单的代码,我想我做错了什么。。。我有2个文件(用于2个类): Poney.js: const {DeadPool} = require('./DeadPool'); class Poney { constructor() { this.regInter = setInterval(() => this.Regeneration(), 500); this.energy = 0

我从node.js开始,我只想在我的类A中使用我的类B,但它似乎不起作用。。。这是我非常简单的代码,我想我做错了什么。。。我有2个文件(用于2个类):

Poney.js:

const {DeadPool} = require('./DeadPool');

class Poney {


  constructor() {
    this.regInter = setInterval(() => this.Regeneration(), 500);
    this.energy = 0;
    this.isUnicorn = false;
  }

  Regeneration() {
    this.energy += 10;
    console.log(`Regeneration, Vitalite : ${this.Vitalite}`);
    if (this.energy > 100) {
      console.log('Evolution');
    }
  }

  KillPoney() {
    clearInterval(this.RegInter);
  }

  evolve() {
    return new Promise((resolve, reject) => {
      setTimeout(() => {
        if (this.isUnicorn = false) {
          console.log('pas une licorne');
          if(this.energy > 100) {
            resolve();
            this.energy = 0;
            this.isUnicorn = true;
            console.log('Transformation unicorn !');
          }
          else {
            console.log('Energie insuffisante');
            reject();
          }
        }
        else {
          reject();
          console.log('Already an unicorn');
        }
      }, 1000);
    });


  }


  backPoney() {                    //Retransforme les licornes en poney
    return new Promise((resolve, reject) => {
      setTimeout(() => {
        if (this.isUnicorn = false) {
          resolve();
          this.energy = 0;
          console.log('Back Poney to Unicorn');
        }
        else {
          reject();
          console.log('Not an Unicorn');
        }


      }, 1000);
    });
  }
}

const myDeadpool = new DeadPool();



//setTimeout(() => MyPoney.killPoney(), 10000);

module.exports = {Poney};
DeadPool.js:

const {Poney} = require('./Poney');

class DeadPool {


  constructor() {

    this.regInterDead = setInterval(() => this.regeneration(), 4000 );
    this.transInter = setInterval(() => this.makeUnicorn(), 4000 );
    this.isRegenerate = false;
    this.poneys = [];
    for (var iVal = 0; iVal < 10; iVal++) {

      this.poneys.push(new Poney())
    }

  }
  makeUnicorn(){
    this.makeEvolve()
      .then(() =>  console.log('Evolve ok !'))
      .catch(() => console.log('error evolve'));
  }

  regeneration(){
    this.checkPoney()
      .then(() => console.log('Regeneration Deadpool'))
      .catch(() => console.log('Pas de regeneration'));
  }



  checkPoney() {
    return new Promise((resolve, reject) => {
      setTimeout(() => {
        var iNumPoney = Math.floor((Math.random() * 10) + 1);
        var bConard = (Math.floor((Math.random() * 1) + 1) >= 1);      // 1 chance sur 2 d'utiliser la licorne
        if (bConard){
          this.poneys[iNumPoney].backPoney()
            .then(() =>  console.log('Licorn to Poney ok'))
            .catch(() => console.log('back licorn rejected'));
          this.isRegenerate = true;
          resolve();
        }

        else{
          console.log('Je ne suis pas un conard');
          reject();
        }
      }, 1000);
    });
  }


  makeEvolve() {
    return new Promise((resolve, reject) => {
      setTimeout(() => {
        var iNumPoney = Math.floor((Math.random() * 10) + 1);
        var bGentil = (Math.floor((Math.random() * 1) + 1) > 1);      // 1 chance sur 2 de la faire evoluer
        if(bGentil) {
          poneys[iNumPoney].evolve()
            .then(() =>  console.log('TRANSFORMATION !'))
            .catch(() => console.log('Evolution impossible'));
          resolve();

        }
        else {
          console.log('Pas gentil');
          reject();
        }


      }, 1000);
    });


  }
}


module.exports = {DeadPool};
const{Poney}=require('./Poney');
类死区{
构造函数(){
this.regInterDead=setInterval(()=>this.regeneration(),4000);
this.transInter=setInterval(()=>this.makeUnicorn(),4000);
this.isRegenerate=false;
this.poneys=[];
对于(var iVal=0;iVal<10;iVal++){
this.poneys.push(新的Poney())
}
}
makeUnicorn(){
this.makeEvolve()
。然后(()=>console.log('Evolve ok!'))
.catch(()=>console.log('error evolve');
}
再生(){
这个。checkPoney()
.然后(()=>console.log('再生死区'))
.catch(()=>console.log('Pas de regeneration');
}
checkPoney(){
返回新承诺((解决、拒绝)=>{
设置超时(()=>{
var iNumPoney=Math.floor((Math.random()*10)+1);
var bConard=(Math.floor((Math.random()*1)+1)>=1);///1机会在独角兽2号上
如果(b康纳){
this.poneys[iNumPoney].backPoney()
.然后(()=>console.log('Licorn to Poney ok'))
.catch(()=>console.log('back-licorn-rejected');
this.isRegenerate=true;
解决();
}
否则{
console.log('Je ne suis pas un conard');
拒绝();
}
}, 1000);
});
}
makeEvolve(){
返回新承诺((解决、拒绝)=>{
设置超时(()=>{
var iNumPoney=Math.floor((Math.random()*10)+1);
var bGentil=(Math.floor((Math.random()*1)+1)>1);///1公平进化2号机会
如果(bgenti){
小马[iNumPoney].evolve()
。然后(()=>console.log('TRANSFORMATION!'))
.catch(()=>console.log('Evolution impossable');
解决();
}
否则{
console.log('Pas gentil');
拒绝();
}
}, 1000);
});
}
}
module.exports={DeadPool};
当我尝试实例化新Poney时,Poney的构造函数中有一个错误:

*"C:\Program Files (x86)\JetBrains\WebStorm 2016.3.3\bin\runnerw.exe" "C:\Program Files\nodejs\node.exe" C:\Users\Stef\Desktop\Cours\JS\Poney.js
C:\Users\Stef\Desktop\Cours\JS\DeadPool.js:19
      this.poneys[iVal] = new Poney();
                          ^
TypeError: Poney is not a constructor
    at new DeadPool (C:\Users\Stef\Desktop\Cours\JS\DeadPool.js:19:27)
    at Object.<anonymous> (C:\Users\Stef\Desktop\Cours\JS\Poney.js:74:20)
    at Module._compile (module.js:571:32)
    at Object.Module._extensions..js (module.js:580:10)
    at Module.load (module.js:488:32)
    at tryModuleLoad (module.js:447:12)
    at Function.Module._load (module.js:439:3)
    at Module.runMain (module.js:605:10)
    at run (bootstrap_node.js:422:7)
    at startup (bootstrap_node.js:143:9)*
*“C:\Program Files(x86)\JetBrains\WebStorm 2016.3.3\bin\runnerw.exe”“C:\Program Files\nodejs\node.exe”C:\Users\Stef\Desktop\Cours\JS\Poney.JS
C:\Users\Stef\Desktop\Cours\JS\DeadPool.JS:19
this.poneys[iVal]=new-Poney();
^
TypeError:Poney不是构造函数
在新的死池(C:\Users\Stef\Desktop\Cours\JS\DeadPool.JS:19:27)
反对。(C:\Users\Stef\Desktop\Cours\JS\Poney.JS:74:20)
在模块处编译(模块js:571:32)
在Object.Module.\u extensions..js(Module.js:580:10)
在Module.load(Module.js:488:32)
在tryModuleLoad时(module.js:447:12)
在Function.Module.\u加载(Module.js:439:3)
位于Module.runMain(Module.js:605:10)
运行时(bootstrap_node.js:422:7)
启动时(bootstrap_node.js:143:9)*

谢谢你的帮助

正如我所评论的,您的通知引发了一个问题

在实例化DeadPool时,尝试将Poney类传递到DeadPool中

  • Remove
    const{Poney}=require('./Poney')来自死池
  • 更改
    const myDeadpool=new DeadPool()
    常量myDeadpool=新的DeadPool(Poney)
  • 将死池构造函数更改为接受Poney
    构造函数(Poney){

  • 有很多方法可以做到这一点,这只是一个基本示例。

    您需要的是循环引用。我会尽量避免这种情况。一个文件开始执行,在它实例化任何内容之前,它会启动并尝试执行另一个文件。另一个文件也会执行相同的操作。两个导出都尚未创建,因此您将一无所获在需要结果的情况下,我们可以互相帮助