Last active
October 16, 2018 21:21
-
-
Save aledwassell/666fb906c41925c733db4418526bc934 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
//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