Last active
March 17, 2016 09:54
-
-
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.
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 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](); | |
} | |
} | |
} | |
} | |
})(); |
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
//self-initialized mixin composition function | |
Vehicle(MixinStore).init() |
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 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' | |
} | |
})(); |
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 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 | |
} | |
})(); |
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 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); | |
} | |
} | |
} | |
} | |
})(); |
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 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