Created
March 16, 2016 04:30
-
-
Save JasonDeving/9be6d1ff914aae4d054b to your computer and use it in GitHub Desktop.
function constructors and looping
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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