javascript 游戏原型继承,构造函数

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了javascript 游戏原型继承,构造函数相关的知识,希望对你有一定的参考价值。

function GameObject(gameAttr) {
  this.createdAt = gameAttr.createdAt;
  this.dimensions = gameAttr.dimensions;
}

GameObject.prototype.destroy = function destroy() {
  return 'Game object was removed from the game.';
};

function NPC(npcAttr) {
  GameObject.call(this, npcAttr);
  this.hp = npcAttr.hp;
  this.name = npcAttr.name;
}

NPC.prototype = Object.create(GameObject.prototype);

NPC.prototype.takeDamage = function takeDamage() {
  return `${this.name} took damage.`;
};

function Humanoid(humAttr) {
  NPC.call(this, humAttr);
  this.faction = humAttr.faction;
  this.weapons = humAttr.weapons;
  this.language = humAttr.language;
}

Humanoid.prototype = Object.create(NPC.prototype);

Humanoid.prototype.greet = function greet() {
  return `${this.name} offers a greeting in ${this.language}.`;
};

module.exports = {
  GameObject,
  NPC,
  Humanoid,
};

以上是关于javascript 游戏原型继承,构造函数的主要内容,如果未能解决你的问题,请参考以下文章

JavaScript继承基础讲解,原型链借用构造函数混合模式原型式继承寄生式继承寄生组合式继承

JavaScript构造函数和原型原型链及this指向

JavaScript 原型继承原型链

浅谈Javascript中的原型原型链继承

JavaScript 对象

JavaScript继承的6种方式