admin管理员组文章数量:1356413
I am currently doing the Codeacdemy tutorials on Javascript and while doing the Object tutorial I came I keep getting undefined for the following:
// Our Person constructor
function Person (name, age){
this.name = name;
this.age = age;
}
// Now we can make an array of people
var family = [];
family[0] = Person("alice", 40);
family[1] = Person("bob", 42);
family[2] = Person("michelle", 8);
family[3] = Person("timmy", 6);
// loop through our new array
for(var person in family){
console.log("name: "+person.name);
}
I have had issues with codecademy before so I tried it in my own webpage and still get undefined. Can anyone explain why to me. I have also tried using family[0].name
and that is undefined too
I am currently doing the Codeacdemy tutorials on Javascript and while doing the Object tutorial I came I keep getting undefined for the following:
// Our Person constructor
function Person (name, age){
this.name = name;
this.age = age;
}
// Now we can make an array of people
var family = [];
family[0] = Person("alice", 40);
family[1] = Person("bob", 42);
family[2] = Person("michelle", 8);
family[3] = Person("timmy", 6);
// loop through our new array
for(var person in family){
console.log("name: "+person.name);
}
I have had issues with codecademy before so I tried it in my own webpage and still get undefined. Can anyone explain why to me. I have also tried using family[0].name
and that is undefined too
5 Answers
Reset to default 4for-in loops return the index, not the value. If you change that to console.log("name: "+family[person].name)
, it will work as expected.
for(var person in family){
console.log(person);
console.log("name: "+person.name);
}
0
name: undefined
1
name: undefined
2
name: undefined
3
name: undefined
As @basilikum also mentioned, you'll need to create each person with the new
keyword, otherwise they won't be an object.
console.log(Person("alice", 40)); // undefined
console.log(new Person("alice", 40)); // Person {name: "alice", age: 40}
You need to create your objects using the new
keyword:
family[0] = new Person("alice", 40);
Person
is just a function. If you call it, you receive whatever this function returns. Since it doesn't return anything, all your entries are undefined. By using new
, you are calling this function as a constructor, which creates an new objects with your defined properties and returns that object instead.
As SpenserJ said, you also have to keep in mind, that the for
loop only returns the key and not the actual object.
Use .push()
to add new array members dynamically, so replacing your following code:
family[0] = Person("alice", 40);
for this one:
family.push( Person("alice", 40) );
A foreach loop in JavaScript provides the array key rather than value. By using family[person].name
instead of person.name
, the code should work.
Edit: The new
keyword also seems to be missing from the Person
creations.
So, I tried the family[person].name on the code academy exercise and it didn't work.
The thing about code academy is that they are looking for an answer SPECIFIC to what they have taught you so far. My bf is a sr. software engineer and the .push tack would totally work in the every day coding world! But they haven't taught us that yet. So, when I plugged it in the code academy still didn't let me through. lol.
Instead I did this:
var family = new Array();
family[0] = new Person("alice", 40);
family[1] = new Person("bob", 42);
family[2] = new Person("michelle", 8);
family[3] = new Person("timmy", 6);
for ( i = 0; i < family[4] ; i++ );
console.log(family[0].name);
console.log(family[1].name);
console.log(family[2].name);
console.log(family[3].name);
And it worked.... I'm sure there are other ways (hopefully shorter) to do this...
本文标签: javascriptWhy is my object undefined when I try to print it when it is clearly definedStack Overflow
版权声明:本文标题:javascript - Why is my object undefined when I try to print it when it is clearly defined? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743984008a2571199.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论