Monday 15 April 2013

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


I am currently doing codeacdemy tutorials on javascript and while doing the object tutorial I am undetermined for the following: // Our person constructor function person (name, age) {this.name = name; This.age = age; } // Now we can create an array of people family = []; Family [0] = person ("Alice", 40); Family [1] = person ("Bob", 42); Family [2] = person ("Michelle," 8); Family [3] = person ("timmy", 6); // Separate Individuals for Families (Loop) through our new array {console.log ("name:" + person.name); }

Before I had problems with codecadi, so I tried to do this in my own webpage and still be undefined. Can anyone explain to me why I have also tried to use the family [0] .name and that is also undefined

is not a value, if you change it in console.log (" name: "+ [family] [.name]), then it Will work as expected.

 for  (family in different person) {console.log (person); Console.log ("name:" + person.name); } 0 Name: Undefined 1 name: Undefined 2 name: Undefined 3 name: Undefined   

as well as @ Basilica, you should call each person new keyword, otherwise They will not be an object.

  console.log (person ("Alice", 40)); // undefined console.log (new person ("Alice", 40)); // person {name: "Alice", age: 40}    

No comments:

Post a Comment