最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Why is my object undefined when I try to print it when it is clearly defined? - Stack Overflow

programmeradmin3浏览0评论

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

Share Improve this question asked Jun 22, 2013 at 16:12 jonniejonnie 12.7k17 gold badges57 silver badges94 bronze badges
Add a ment  | 

5 Answers 5

Reset to default 4

for-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...

发布评论

评论列表(0)

  1. 暂无评论