此刻我正在学习javascript,使用相同的代码遇到2个问题。
1)如果将大括号放在var Character function
下的代码顶部,请使用大括号。我可以在控制台中使用changedXPos();
命令,如果不这样做,我会得到changedXPos();
这个错误“未捕获的ReferenceError:未定义的XPos为:1:1”我不是确定原因。
2)在运行代码的changedXPos
函数中,我得到的值为NaN。我使用了调试器,可以看到xPos
的所有实例都是未定义的。如果我提供xPos = 20
的代码可以正常工作,那么我知道xPos
出于某种原因并没有表现出来,我不知道为什么。
我已在代码中添加注释以显示问题出在哪里。 谢谢您的时间
var Character = function(name, xPos, yPos) {
this.name = name;
this.xPos = xPos;
this.yPos = yPos;
//} this bracket is not commentented out code works, by changedXPos(); in console but xPos is still undefined
//create instance of Character
var ron = new Character("Ronald", 55, 30);
var jil = new Character("Jill", 25, 45);
var jas = new Character("Jasmine", 16, 85);
//create arrary of instance Character
var characterArray = [ron, jil, jas];
//create for loop to loop through characterArray
for (i = 0; i < characterArray.length; i++) {
console.log(characterArray[i]);
}
this.information = function() {
"My name is: " + this.name + " My X position is: " + this.xPos + " My Y position is: " + this.yPos;
}
this.changedXPos = function(value) {
// change the x position here
//debugger;
var xPos = this.xPos; // if i take this var out i get xPos is undefined
//var value = isNaN(parseInt(xPos)) ? 0 : parseInt(xPos);
for (i = 0; i < characterArray.length; i++) {
value = xPos + 20;
console.log(value); // value is NaN or xPos is undefined
}
}
this.changedYPos = function(value) {
// change the y position here
}
Character.prototype.toString = function toString() {
//var info = // character's name and current position on the screen
//return info;
};
} // with this bracket coded out above function is out of code block
答案 0 :(得分:0)
似乎是您想要修复代码的地方。要带您过去,您创建了构造函数Function,然后创建了3个实例,分别名为ron
,jil
和jas
然后,在Character的原型链上添加了许多函数,这些函数重写了toString方法,这会打印出您的自定义方法。
堆栈溢出时的可运行脚本似乎可以打印整个对象,但是如果您在代码中运行它,则应该可以正常工作。
function Character (name, xPos, yPos) {
this.name = name;
this.xPos = xPos;
this.yPos = yPos;
}
var ron = new Character("Ronald", 55, 30);
var jil = new Character("Jill", 25, 45);
var jas = new Character("Jasmine", 16, 85);
var characterArray = [ron, jil, jas];
Character.prototype.changedXPos = function(value) {
this.xPos = value;
}
Character.prototype.changedYPos = function(value) {
this.yPos = value;
}
Character.prototype.toString = function() {
return "My name is: " + this.name +
" My X position is: " + this.xPos +
" My Y position is: " + this.yPos;
};
//create for loop to loop through characterArray
for (i = 0; i < characterArray.length; i++) {
console.log(characterArray[i]);
}
答案 1 :(得分:0)
查看下面的代码,然后检查这是否是您要实现的目标。
function Character(name, xPos, yPos) {
this.name = name;
this.xPos = xPos;
this.yPos = yPos;
this.changedXPos = function(value) {
this.xPos = this.xPos + value;
}
this.changedYPos = function(value) {
this.yPos = this.yPos + value;
}
this.information = function() {
return "My name is: " + this.name + " My X position is: " + this.xPos + " My Y position is: " + this.yPos;
}
}
var ron = new Character("Ronald", 55, 30);
var jil = new Character("Jill", 25, 45);
var jas = new Character("Jasmine", 16, 85);
var characterArray =[ron,jil,jas];
function changedXPosition(value) {
for(i = 0; i < characterArray.length; i++){
characterArray[i].changedXPos(value);
}
}
function changedYPosition(value){
for(i = 0; i < characterArray.length; i++){
characterArray[i].changedYPos(value);
}
}
changedXPosition(20);
changedYPosition(30);
for(i = 0; i < characterArray.length; i++){
console.log(characterArray[i].information());
}