#27 Understanding Array of Objects and the loop -


#1


_Can someone explain what is the name of the Objects that are in the array ? I thought to name an object we would create an object and name it like this: so that bob would be the name of the object_**

var bob = new Person (age, name);

**But i do not understand how creating the objects like below gives the object a name to be placed in the new array - ? yes the properties using dot notation i can understand -

but alice nor bob nor michelle is the name in the array - ok i see we set the index place for an object (a Person) but if i console.log (family[0]) the object would not be alice it's property would be, but not the object -- PLEASE EXPLAIN what's happening -- MANY THANKS**


function Person(name, age){
    this.name = name;
    this.age = age;
}
var family = new Array()

family [0] = new Person ("alice", 40);
family [1] = new Person ("bob", 42);
family [2] = new Person ("michelle", 8);


#2

what you do here:

var bob = new Person (age, name);

is creating an instance of an object, and store it in variable called bob. bob is just a variable holding an instance of an object. when you do this:

family [0] = new Person ("alice", 40);

you creating an instance of an object, but instead of storing it in a variable, you store it in an array

in this case, the instances don't have variable names, they are just stored in an array and you can access them through array + index.


#3

Thanks for Speedy Response - Liggght Bullb!! lol

So the way i see it, is that we are storing instances of the objects we create in an array - where we can access the lesser properties of the object through dot.notation, But the main category (or Array) THE 'FAMILY' is what is important.

And all members who hold a place (index value) in the family (array) will have attributes (properties) that can be accessed through array + index then dot. notation (family[x].age)

(It helps when i can conceptualize what is going on- forgive me)

BIG THANK YOU!!


#4

yes, so far so good.

you can always access property's of objects through the dot notation. but if have a variable which holds the instance of the object you could for example access age in the following way:

var bob = new Person (42, "Bob");
console.log(bob.age) # will log 42

if the instance is however stored in an array:

family [0] = new Person ("alice", 40);

we need to specify the array + index instead of a variable, and then we can use the dot notation to access age:

console.log(family[0].age) # will log 40

oops, i should have read your reply first, it seems you understand this already:

you seem to understand completely :slight_smile:


#5

Good to Go! Many thanks once again!


#6

yep, you understand everything taught in this exercise, you can safely move on. Good luck!


#7