为什么这一直在说它没有被定义

时间:2016-05-30 00:09:43

标签: javascript function object methods

使用firebug它一直声明this.speak没有定义,我不明白为什么会这样。我正在尝试将它输出到屏幕

 $(document).ready (function() {


    function person(rank, number, id) {
        //properties
        this.intRank = rank;
        this.intNumber = number;
        this.strId = id;
        this.elOutput = document.getElementById(id);
        //methods
        this.speak = fucntion(); {
            this.elOutput.innerHTML += "<br>" + this.strId;
        };

        //adds one to int number and speaks message
        this.pickUpNumber = function() {
            this.intNumber++;
            this.strId = "i have" + this.intNumber.toString() + "rocks";
            this.speak();
        };
    };

    //object

    var Jak = new person(5, "hey ,jack", " Captain");
    var Cap = new person(3, "yea cap?", "jacko");

    jak.speak(); cap.speak(); jak.pickUpRock();

});

1 个答案:

答案 0 :(得分:3)

this.speak = fucntion(); {
  this.elOutput.innerHTML += "<br>" + this.strId;
};

我认为你的意思是 function()

JS是区分大小写的。

这可能是你想要完成的事情:

$(document).ready (function() {
    function person(rank, number, id) {
      //properties
      this.intRank = rank;
      this.intNumber = number;
      this.strId = id;
      this.elOutput = document.getElementById(id);


      //methods
      this.speak = function() {
        this.elOutput.innerHTML += "<br>" + this.strId;
      };

      //adds one to int number and speaks message
      this.pickUpNumber = function() {
        this.intNumber++;
        this.strId = "i have" + this.intNumber.toString() + "rocks";
        this.speak();
      };
    }

    //object

    var jak = new person(5, "hey ,jack", " Captain");
    var cap = new person(3, "yea cap?", "jacko");

    jak.speak(); cap.speak(); jak.pickUpNumber();
});

结帐this正在运行的jsbin