Skip to content

Instantly share code, notes, and snippets.

@hwclass
Last active March 17, 2016 09:54
Show Gist options
  • Save hwclass/60a0d829614c0e2e457e to your computer and use it in GitHub Desktop.
Save hwclass/60a0d829614c0e2e457e to your computer and use it in GitHub Desktop.
Mixin Builder : An example using compositional inheritance to create new functionalities by mixin curations.
var App = (function () {
return {
init : function (modules) {
for (var moduleIndex = 0, len = modules.length; moduleIndex < len; moduleIndex++) {
for (var prop in modules[moduleIndex]) {
modules[moduleIndex][prop]();
}
}
}
}
})();
//self-initialized mixin composition function
Vehicle(MixinStore).init()
var MessageStore = (function MessageStore () {
return {
HOLDING_THE_WHELL : 'holding whell...',
FIRING_THE_ENGINE : 'firing the engine...',
FULLING_THE_TANK : 'fulling the tank...',
HOLDING_THE_STICK : 'holding the stick'
}
})();
var MixinBuilder = (function () {
var mixinCollection = {};
var create = function (mixins) {
if (!!mixins) {
for (var mixinIndex = 0, len = mixins.length; mixinIndex < len; mixinIndex++) {
for (var attrName in mixins[mixinIndex]) {
mixinCollection[attrName] = mixins[mixinIndex][attrName];
}
}
}
return mixinCollection;
}
return {
create : create
}
})();
var MixinStore = (function MixinStore () {
return function (MessageStore) {
return {
driving : {
holdTheWhell : function () {
console.log(MessageStore.HOLDING_THE_WHELL);
},
fireTheEngine : function () {
console.log(MessageStore.FIRING_THE_ENGINE);
},
fullTheTank : function () {
console.log(MessageStore.FULLING_THE_TANK);
}
},
cycling : {
holdTheStick : function () {
console.log(MessageStore.HOLDING_THE_STICK);
}
}
}
}
})();
var Vehicle = (function () {
return function (mixinStore) {
var self = MixinBuilder.create([mixinStore.driving, mixinStore.cycling]);
return {
init : function () {
self.holdTheWhell(); // logs holding the whell
self.fireTheEngine(); // logs firing the engine
self.fullTheTank(); // logs fulling the tank
self.holdTheStick(); //logs holding the stick
}
}
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment