Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/386.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/2/node.js/39.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_Node.js_Class_Oop - Fatal编程技术网

用javascript正确组织类

用javascript正确组织类,javascript,node.js,class,oop,Javascript,Node.js,Class,Oop,我正在node.js中为游戏编写服务器 我对正确的构造有问题。例如,我有以下代码: class mob { constructor(name) { this.name = name; this.health = 100; } getHealth() { return this.health; } } class items{ constructor(name, id) { this.name = name;

我正在node.js中为游戏编写服务器

我对正确的构造有问题。例如,我有以下代码:

class mob {
    constructor(name) {
      this.name = name;
      this.health = 100;
    }
    getHealth() {
      return this.health;
    }
}

class items{
    constructor(name, id) {
      this.name = name;
      this.id= id;
      this.value= value;
    }
    getValue() {
      return this.value;
    }
}

 class world {
    constructor(name) {
      this.name = name;
      this.weather = 'rain';
      this.monsters = {};
    }

   addMonster(name) {
     const monster = new mob(name);
     this.monsters['1'] = monster;
   }

   setWeather(weather) {
     this.weather = weather;
   }
   play() {
     this.addMonster('dragon');

   }
 }

game = new world;
game.play();

我想在一个世界级中操纵天气,而不是在monster类中使用如下示例代码复制天气:
monster.setWeather(“雪”)

除此之外,我还想在一个世界类中添加新类,如项目、映射对象等,并使用世界类中的函数对它们进行操作。例如:
item.setWeather(“彩虹”)并在其他类上操作类,如
monster.getItemValue('item')

在javascript中这样做的正确方法是什么?

我不知道我是否理解正确


由于您已经有一个全局变量“game”,因此可以直接使用“game.setWeather()。

欢迎使用堆栈溢出!请拿着(你得到了一个徽章!),四处看看,仔细阅读,特别是我还推荐乔恩·斯基特的。恐怕这对于SO的问答形式来说太宽泛和开放了。我建议通过一些基本的JavaScript教程和/或一本好的入门书来学习基础知识
不是添加到
怪物
阵列的正确方法;它总是在索引1处放置一个怪物(留下索引0和索引2未被占用)。要向数组添加条目,通常使用以下方法:
this.monsters.push(newmob(name)),尽管你看到的东西像
this.monsters[this.monsters.length]=新怪物(名字)(我不建议这样做)。此外,JavaScript中压倒性的惯例是构造函数的名称(如你的
mob
、和
世界
)以大写字母开头;所以,
Mob
项目
,以及
世界
。最后,请注意,对象的所有属性本质上都是公共的,因此不需要
getHealth
getValue
setWeather
;您可以直接使用
health
value
weather
。(很快将添加到
语法中。它们不是属性,尽管它们很相似。)对,当然是好的观点。谢谢你的更正!是的,但是我想在一个世界(游戏)类中以这种方式进行操作,我想要这样的东西:`class mob{constructor(name){this.name=name;this.health=100;}getHealth(){return this.health;}setWeather(weather){game.setWeather(weather)}“但是我得到一个错误,游戏不是一个声明的对象。好吧,你的解决方案是好的,问题很可能是因为我使用了两个单独的文件,所以我不能以那种方式使用它。