[英]JavaScript returning NaN variable returning undefined
我目前正在学习JavaScript,使用相同的代码有2个问题。
1)如果我在var Character function
下的代码顶部放置一个,则用大括号括起来。 我可以得到changedXPos();
命令在控制台中工作,如果不这样做,我会被changedXPos();
此错误“未捕获的ReferenceError:未在:: 1:1处定义changedXPos”我不确定为什么。
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
看起来这就是您要修复的代码。 为了带您过去,您创建了构造函数Function,然后创建了3个实例,分别名为ron
, jil
和jas
然后,在Character的原型链上添加了一堆函数,这些函数重写了toString方法,这会打印出您的自定义方法。
Stack溢出时的可运行脚本似乎可以打印整个对象,但是如果您在代码中运行它,它应该可以工作。
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]); }
查看下面的代码,并检查这是否是您要实现的目标。
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()); }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.