【问题标题】:prototype object/method code - not displaying on cosole原型对象/方法代码 - 不在控制台上显示
【发布时间】:2016-05-23 22:09:59
【问题描述】:

所以这段代码是用于我的 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"
})

【问题讨论】:

    标签: javascript function methods console prototype


    【解决方案1】:

     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");

    【讨论】:

    • 您传递给创建 Pokemon 对象的参数 {...} 是一个对象而不是 5 个数据。它被识别为一个参数 - “名称”,而不是 5 个参数。所以剩下的参数 type1, type2, height, weight 都是未定义的。
    • tinypic.com/r/sl4rj9/9 好了,这是我的问题。我没有得到任何具体信息,仅此而已。哈哈
    • 我很尴尬。我的代码确实有效,只是我没有把我的 () 放在我的函数调用之后。
    • 永远不要为任何类型的错误感到难过:)
    猜你喜欢
    • 1970-01-01
    • 2022-06-14
    • 2022-12-18
    • 1970-01-01
    • 2011-04-20
    • 2020-09-17
    • 1970-01-01
    • 1970-01-01
    • 2021-03-25
    相关资源
    最近更新 更多