Skip to content

Instantly share code, notes, and snippets.

@aledwassell
Last active October 16, 2018 21:21
Show Gist options
  • Save aledwassell/666fb906c41925c733db4418526bc934 to your computer and use it in GitHub Desktop.
Save aledwassell/666fb906c41925c733db4418526bc934 to your computer and use it in GitHub Desktop.
//Prototypal inhericance uses the the Object.create() syntax, remember this!!!!!
let Human = {
name: "David",
create: function(value){
let instance = Object.create(this);
Object.keys(value).forEach(function(key){
instance[key] = value[key];
})
return instance;
},
likes: [],
sayName: function(){
console.log(this.name);
},
sayLikes: function(){
if(this.likes.length > 1){
for(let i = 0; i < this.likes.length; i++){
console.log(this.likes[i])
}
} else {
console.log(this.likes[0])
}
}
}
let musician = Object.create(Human)
musician.instrument = "Sax";
musician.sayInstrument = function(){
console.log(this.instrument)
}
//then you can use the create function to make a new musician
let mike = musician.create({instrument:"Basoon", name:"Mike", likes:["dough", "pizza"]})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment