jonnie
jonnie

Reputation: 12680

Why is my object undefined when I try to print it when it is clearly defined?

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

Upvotes: 2

Views: 3713

Answers (5)

Amy
Amy

Reputation: 1

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

Upvotes: 0

SpenserJ
SpenserJ

Reputation: 802

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}

Upvotes: 4

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) );

Upvotes: 2

basilikum
basilikum

Reputation: 10528

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.

Upvotes: 2

tvkanters
tvkanters

Reputation: 3523

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.

Upvotes: 1

Related Questions