objects output with for loops

objects are passed by a reference, where as variables are passed by value.

by Adam Kinnucane

JavaScript

debugger;
//default person object.
var defperson = {"age":20,"company":"Holograph","pet":{"name":"mushy"}};
//array of names
var names = ["ben","bert","fred"]
//empty array to be pushed into
var people = [];
//for loop set to run through 3 iterations.
for(var x=0;x<3;x++){
	//empty object for the iterations to be pushed into.
	var person = {};
	//p for property is each key, which outputs the value of that key, iterated through 3 times
  for(var p in defperson){
  //the defpersons properties are now being stored into the person object this is also iterated 	through 3 times as its within the for loop, really helps to usae debugger to understand the 		process. 
  	person[p] = defperson[p];
  }
  //adding the names from the array into the person object
	person.name  = names[x];
	//trying to add a pet name won't work as its an object within an object which is trying to 		be accessed by a value.
  person.pet.name = "animal belongs to " + person.name;
  //push the three person objects into the empty people array.
	people.push(person);
}
//log the people array which contains 3 person objects with their properties.
console.log( people );