【问题标题】:How can i select all javascripts objects?如何选择所有 javascripts 对象?
【发布时间】:2021-10-21 09:30:18
【问题描述】:

这是我的课:

class Player {
  static list = new Map()
  constructor(id) {
    Player.list.set(id, this)
    this.id = id;
    this.x = 0;
    this.y = 0;
    ...
  }
}

如何循环每个玩家?谢谢解答

【问题讨论】:

标签: javascript list class constructor


【解决方案1】:

您可以像这样遍历 Map 值:

for(let player of Player.list.values()) {
  player.update();
  player.getUpdatePack();
}

【讨论】:

    【解决方案2】:

    由于您使用的是要循环的地图,因此您可以使用以下示例:

    Player.list.forEach(player => {
        players.update();
        players.getUpdatePack();
    })
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-12-08
      • 2023-01-13
      • 1970-01-01
      • 2010-11-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多