OOP 核心-多态

JavaScript 没有实现对方法重载的支持,我们可以通过判断参数的个数和类型进行方法的重载

Person.prototype.walk = function(speed, direction){
  if(arguments.length == 1){
    console.log(this.name + " is walking by " + speed + "km/s");
  }else{
    console.log(this.name + " is walking to " + direction +" by " + speed + "km/s");
  }
}

lee.walk(1000);// Lee is walking by 1000km/s
lee.walk(1000,'east');// Lee is walking to east by 1000km/s

results matching ""

    No results matching ""