Created
August 25, 2016 12:21
-
-
Save mdix/25beb32e2b7b7b2dde6383e2f336ada0 to your computer and use it in GitHub Desktop.
Mixin that doesn't know anything about the object it's mixed into. Check http://disq.us/p/1ba1awd for discussion
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
var Person = { | |
init: function(name) { | |
this.name = name; | |
}, | |
doSomethingAndUseSpeak: function(text) { | |
this.speak({name: this.name, text}) | |
} | |
}; | |
var canSpeak = { | |
speak: function(conf) {console.log(conf.name + ': ' + conf.text)} | |
}; | |
var bob = Object.assign(Object.create(Person), canSpeak) | |
bob.init("Bob the Builder") | |
bob.doSomethingAndUseSpeak('Can we fix it? Yes! We can...') | |
var jack = Object.assign(Object.create(Person), canSpeak) | |
jack.init("Jack Sparrow") | |
jack.doSomethingAndUseSpeak('The seas may be rough, but I am the Captain! No matter how difficult I will always prevail.') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment