Skip to content

Instantly share code, notes, and snippets.

@JasonDeving
Created March 16, 2016 04:30
Show Gist options
  • Save JasonDeving/9be6d1ff914aae4d054b to your computer and use it in GitHub Desktop.
Save JasonDeving/9be6d1ff914aae4d054b to your computer and use it in GitHub Desktop.
function constructors and looping
// We are creating a constructor
function AnimalMaker(name) {
return {
speak: function () {
console.log("my name is ", name);
},
name: name,
owner: "jason"
};
};
// Here is a list of animal names
var animalNames = ["Sheep", "Liger", "Big Bird"];
// we create an array to store all the objects
var farm = [];
// we loop through and push the objects into farm
for(var i = 0; i < animalNames.length; i++) {
var animal = AnimalMaker(animalNames[i]);
farm.push(animal);
}
// loop through all the animals
for(var i = 0; i < farm.length; i++) {
farm[i].speak();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment