Jump to content

call method of object while looping thru instances of objects

Recommended Posts

function Person (name, age) { this.name = name;    this.age = age;this.printMe = new function(){ return console.log("name" + Person.name + "age" + Person.age);      };}// Now we can make an array of peoplevar 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);// add the last family member, "timmy", who is 6 years oldfor (i=0; i < family.length; i++){ family[i].printMe;   }

It's simple I wanna print name and age but this way it isn't working


	Edited  by roberto68

Link to post
Share on other sites

Try removing "new" from the printMe function inside function Person and adding () to family.printMe();


Also in the console.log output, instead of Person.name and Person.age, have instead this.name and this.age.


Oh, also for the sake of good practice, don't forget the keyword 'var' in the for loop declaration. :good:

Edited by Don E
Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...