【发布时间】:2016-09-27 17:16:55
【问题描述】:
使用萤火虫一直说 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();
});
【问题讨论】:
-
错字
this.speak = fucntion==>function -
fucntion之后的分号也放错了位置,您的Jak和Cap变量应该像在声明后的方法调用中一样小写。 -
请记住,在 javascript
this.speak = function();后面紧跟分号,在定义之前终止您的函数语句。因此它将永远是undefined -
@element11:这实际上是一个语法错误。代码甚至不会被执行。
-
您的代码有语法错误。它根本无法运行。如果代码甚至没有执行,调试器如何告诉你
this.speak是undefined?
标签: javascript function object methods