调用现有方法来创建新实例

时间:2016-05-05 10:45:13

标签: methods constructor prototype

需要指导如何编写三个实例,然后使用isWarmBlooded方法为变量赋值。

```Animal.prototype.isWarmBlooded = function(species){

  if(this.species === "Fish"){
    return this.species = false;
  }
  if(this.species === "Monkey" || this.species === "Bird"){
    return this.species = true;
  }
  if(this.species !== "Fish" || this.species !== "Monkey" ||     this.species !== "Bird"){
    return "Could not determine if warm-blooded";
  }
 };
//Call the isWarmBlooded method on three Animal instances
//and assign the values to each variable below.
var warmBloodedAnimal = Animal.prototype.isWarmBlooded("Monkey");
var coldBloodedAnimal;
var notWarmOrColdAnimal;```

1 个答案:

答案 0 :(得分:0)



var Animal = function (species){
  this.species = species;
}; 

Animal.prototype.isWarmBlooded = function(species){ 
  if (this.species === "Fish"){ 
  	return false; 
  }
  else if(this.species === "Monkey" || this.species === "Bird"){ 	
  	return true; 
  }
  else{
  	return "Could not determine if warm-blooded"; 
  }
}; 

var warmBloodedAnimal = new Animal("Monkey");
console.log(warmBloodedAnimal.isWarmBlooded());