使用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();
});
最佳答案
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
关于javascript - 为什么这一直说它没有定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37516119/