Last active
April 13, 2019 16:37
-
-
Save winarcooo/399940ed50367bbe8fdf8fe4ecadf2c9 to your computer and use it in GitHub Desktop.
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
/** | |
* prototype is just way of saying for this object use other object | |
* as a backup/delegate/prototype | |
* if someone call my object with a property that doesn't exist on my object | |
* go lookin this other object | |
*/ | |
function talk() { | |
console.log(this.sound) | |
} | |
let animal = { | |
talk | |
} | |
let dog = { | |
sound: 'woof' | |
} | |
let scoobyDoo = { | |
howl: function() { | |
console.log(this.sound.toUpperCase()) | |
} | |
} | |
Object.setPrototypeOf(dog, animal) | |
Object.setPrototypeOf(scoobyDoo, cat) | |
dog.talk() // woof | |
scoobyDoo.howl() // WOOF |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment