【发布时间】:2014-02-08 12:09:27
【问题描述】:
目前,我在用我在代码中称为athlete 的对象填充我在代码中称为input 的数组时遇到问题。称为athlete 的对象是使用其他几个数组实例化的。我在这篇文章中附加了一个 jsfiddle 链接,它基本上是一个具有相同问题的简化版本。我知道逻辑可能看起来是多余的,但那是因为这个例子是必要的(实际的逻辑将与用户输入一起工作)。
我的问题是我正在用 new Athlete 对象填充数组,但我无法访问数组中对象的特定属性。
我是处理对象的新手,所以我很感激有关如何使这项工作的任何建议。我添加了最后一行代码来显示我的输入数组。
var input = new Array();
var girl=[1,1,1,1];
var boy = [1,1,1,1];
var balleyscore = [100,400,320,50];
var boyHeight = [72,68,65,75];
var girlHeight=[60,57,65,55];
var boyAge = [19,32,22,25];
var girlAge = [20,15,32,18];
//the above are all generate from user inputs
// they go into the object constructor below
function athlete(girl, boy,balleyscore, height, age){
this.girl=girl;
this.boy=boy;
this.balleyscore=balleyscore;
this.height=height;
this.age = age;
};
function insertToObjectArray()
{
var j=0;
var i=0; //insert all our data into one array of the load object
for(j = 0; j < girl.length;j++)
{
input[j]= new athlete(true,false,balleyscore[j],girlHeight[j],girlAge[j]);
}
for(j = girl.length; j <=girl.length+boy.length;j++)
{
input[j]= new athlete(false,true,0,boyHeight[i],boyAge[i]);
i++;
}
};
insertToObjectArray();
document.getElementById("demo").innerHTML=input;
【问题讨论】:
-
I cannot access a specific property of an object in the array.不足以调试程序。 :( -
您要访问哪个属性?究竟是什么不符合您的预期?
-
我需要能够访问每个属性
-
不起作用的是我的数据没有正确存储到运动员对象的属性中。例如,如果您输出 input[1].age 它是未定义的
-
你的对象[运动员]有什么属性?
标签: javascript arrays object constructor