JavaScript Extending Class

前端 未结 10 1374

I have a base class:

function Monster() {
  this.health = 100;
}

Monster.prototype.growl = function() {
  console.log(\"Grr!\");
}

That

10条回答
  •  粉色の甜心
    2020-11-30 18:31

    the absolutely minimal (and correct, unlike many of the answers above) version is:

    function Monkey(param){
      this.someProperty = param;
    }
    Monkey.prototype = Object.create(Monster.prototype);
    Monkey.prototype.eatBanana = function(banana){ banana.eat() }
    

    That's all. You can read here the longer explanation

提交回复
热议问题