Skip to content

Instantly share code, notes, and snippets.

@mdix
Created August 25, 2016 12:21
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mdix/25beb32e2b7b7b2dde6383e2f336ada0 to your computer and use it in GitHub Desktop.
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
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