原型对象/方法代码 - 不显示在cosole上

时间:2016-05-23 22:10:00

标签: javascript function methods console prototype

所以这段代码是针对我的javascript类的,所以它对于网站本身并不是必不可少的,但我仍然被困住了。所以我试图制作一个代码,当我调用某个名称和方法(名称,类型1,类型2,高度或重量)时,我得到了我在代码中提供的答案。我未定义,但在查看我的教师演示时,它可以正常工作。

我希望我不会错过一些非常简单的事情。非常感谢任何帮助!

 var Pokemon = function Pokemon(name , type1 , type2 , height , weight) {
  this.name = name;
  this.type1 = type1;
  this.type2 = type2;
  this.height = height;
  this.weight = weight;
}

Pokemon.prototype.whichPoke = function whichPoke() {
  console.log("Hey, I am " + this.name);
}
Pokemon.prototype.whichType1 = function whichType1() {
  console.log("Hey, I am " + this.type1);
}
Pokemon.prototype.whichType2 = function whichType2() {
  console.log ("Hey, I am " + this.type2);
}
Pokemon.prototype.whichHeight = function whichHeight() {
  console.log ("Hey, I am " + this.height + " inches tall");
}
Pokemon.prototype.whichWeight = function whichWeight() {
  console.log ("Hey, I am " + this.weight + " pounds")
}

var Horsea = new Pokemon({
  name: "Horsea",
  type1: "Water",
  type2: "none",
  height: "16",
  weight: "17.6"
})
var Seadra = new Pokemon({
  name: "Seadra",
  type1: "Water",
  type2: "none",
  height: "47",
  weight: "55.1"
})
var Kingdra = new Pokemon({
  name: "Kingdra",
  type1: "Water",
  type2: "Dragon",
  Height: "71",
  Weight: "335.1"
})

1 个答案:

答案 0 :(得分:3)



 var Pokemon = function Pokemon(name , type1 , type2 , height , weight) {
  this.name = name;
  this.type1 = type1;
  this.type2 = type2;
  this.height = height;
  this.weight = weight;
}

Pokemon.prototype.whichPoke = function whichPoke() {
  console.log("Hey, I am " + this.name);
}
Pokemon.prototype.whichType1 = function whichType1() {
  console.log("Hey, I am " + this.type1);
}
Pokemon.prototype.whichType2 = function whichType2() {
  console.log ("Hey, I am " + this.type2);
}
Pokemon.prototype.whichHeight = function whichHeight() {
  console.log ("Hey, I am " + this.height + " inches tall");
}
Pokemon.prototype.whichWeight = function whichWeight() {
  console.log ("Hey, I am " + this.weight + " pounds")
}

var Horsea = new Pokemon("Horsea", "Water", "none", "16", "17.6");
var Seadra = new Pokemon("Seadra", "Water", "none", "47", "55.1");
var Kingdra = new Pokemon("Kingdra", "Water", "Dragon", "71", "335.1");