Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/369.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
javascript中每个实例化类都有自己的循环_Javascript_Loops_Ecmascript 6 - Fatal编程技术网

javascript中每个实例化类都有自己的循环

javascript中每个实例化类都有自己的循环,javascript,loops,ecmascript-6,Javascript,Loops,Ecmascript 6,我想尝试创建一个参与者的概念证明,在主循环之外有自己的独立循环-我创建了类似的东西,但我想知道是否有一些突出的问题,或者我是否完全错了 基本上,我想知道处理内部循环的正确方法是使用它,还是在live函数中有更好的方法: setTimeout=>{this.live},100 第二个问题是知道用类销毁实例化类的最佳方法。销毁-现在我只是删除从容器到对象的连接 示例如下: 我也将粘贴代码本身: <ul id="simple-log"></ul> <script>

我想尝试创建一个参与者的概念证明,在主循环之外有自己的独立循环-我创建了类似的东西,但我想知道是否有一些突出的问题,或者我是否完全错了

基本上,我想知道处理内部循环的正确方法是使用它,还是在live函数中有更好的方法: setTimeout=>{this.live},100

第二个问题是知道用类销毁实例化类的最佳方法。销毁-现在我只是删除从容器到对象的连接

示例如下:

我也将粘贴代码本身:

<ul id="simple-log"></ul>

<script>
// We will store out actors as "id" => "actor"
let actors = {};

// Custom logging functionality for a simple ul list
const sLog = (text) => {
  let li = document.createElement('li');
  li.innerHTML = text;
  document.getElementById('simple-log').appendChild(li);
};

const randomNum = (min,max) => { return Math.floor(Math.random() * max) + min; }

// Actor definition
class Actor {
  constructor(name, id) {
    this.id = id;
    this.name = name;
    this.gender = randomNum(1,2) === 1 ? 'male' : 'female'; // Random gender
    this.lastTime = null;
  }

  live() {
    // Get the current time, and log every 5 seconds 
    let now = Date.now();
    let difference = now - this.lastTime;
    if(difference > 5000) {
      sLog(`Actor "${this.name}" Log - Difference: ${difference}`);
      this.lastTime = now;
    }

    // Determine if the actor died of a tragic accident
    if(randomNum(1,100000) < 5) {
      // Something tragic happened, that caused this actor to die
      this.die();
    } else {
      // I'm pretty sure that this is not the best way, but for some reason just
      // setTimeout(this.live, 1); does not work
      setTimeout(() => {this.live()}, 100);
    }
  }

  die() {
    // Log the death
    sLog(`Actor "${this.name}" died`);

    // This seems really a wrong way to do this, should not need to rely on an element outside of the scope - something else should do this, but how?
    delete actors[this.id];
  }
}

// Function to spawn a new actor
let spawnActor = () => {
  let id = 'a'+randomNum(1,9000000);
  sLog('Spawning an actor');
  let actorInstance = new Actor(id, id); // Rejoice!
  actorInstance.live();
  actors[id] = actorInstance;
}

// Simple loop that simulates the rendering of the screen
let lastTimeAnimated = null;
let mainAnimationLoop = () => {
  // Logs every 5 seconds to the log
  let now = Date.now();
  let difference = now - lastTimeAnimated;
  if(difference > 5000) {
    sLog(`Main Animation Loop Log - Difference: ${difference}`);
    lastTimeAnimated = now;
  }

  window.requestAnimationFrame(mainAnimationLoop);
}

// Simple loop that simulates a normal game main loop
let lastTime = null;
let mainLoop = () => {
  // Mainloop logs every 5 seconds to the log
  let now = Date.now();
  let difference = now - lastTime;
  if(difference > 5000) {
    sLog(`Main Loop Log - Difference: ${difference}`);
    lastTime = now;
  }

  // Random actor spawner
  if(randomNum(1,10000) < 5) {
    spawnActor(); // It truly is a blessed day!
  }

  setTimeout(mainLoop, 1);
}

// Should be obvious
let init = () => {
  mainAnimationLoop();
  mainLoop();
}

// Let's get started!
init();
</script>
基本上,我想知道处理内部循环的正确方法是使用它,还是在live函数中有更好的方法:setTimeout=>{this.live},100

有许多其他的方法可以做到这一点,其中一些甚至涉及到一个真正的while循环,但没有一个是正确的方法

我很确定这不是最好的方法,但出于某种原因 setTimeoutthis.live,1;不起作用

看看为什么

第二个问题是知道用类销毁实例化类的最佳方法。销毁-现在我只是删除从容器到对象的连接:

delete actors[this.id];
这似乎真的是一种错误的方法,不需要依赖范围之外的元素-其他东西应该这样做,但是如何做呢

您不能破坏javascript中的任何内容。如果希望实例被垃圾回收,则需要删除对它的所有引用。让一个演员死的正确方法是让他停止活下去——也就是说,不要再打电话给.live了,和/或取消所有预定要打电话给他的超时时间


任何东西都不需要这个容器,在您展示的代码中,您甚至没有使用它。出于某种原因,spawnActor确实存储了实例,因此它的工作就是收集死亡的实例。如果你真的不需要这个集合,就忽略它;如果您将if用于某个对象,那么每个参与者都应该通过设置自己的属性或向主要参与者发送消息来宣布其死亡,以便根据需要将其从集合中删除。

不清楚您在问什么。请针对特定代码提出特定问题。我想知道我是否做错了,这不是一个具体的问题。描述一个特定的目标,解释你在实现这个目标时遇到了什么问题,以及你在当前代码中观察到了什么,然后确切地描述你需要什么帮助。