Skip to content

Instantly share code, notes, and snippets.

@5angel
Last active June 11, 2016 10:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 5angel/7a89f1822a01f4d44c8353e98d6f1400 to your computer and use it in GitHub Desktop.
Save 5angel/7a89f1822a01f4d44c8353e98d6f1400 to your computer and use it in GitHub Desktop.
const STORE = {};
export default {
init: function (name, Fn) {
if (typeof name !== 'undefined') {
return STORE[name] = Fn;
}
Fn = STORE[this.root.getAttribute('data-as')];
let instance = Fn && new Fn();
if (instance) {
let _prototype;
let _super = {};
let mixin = _super;
do { // reconstruct prototype chain
let previous = !_prototype ? instance : _prototype;
_prototype = Object.getPrototypeOf(previous);
// skip the first & the last
if (previous !== instance && _prototype !== null) {
Object.getOwnPropertyNames(previous)
.forEach(function (key) {
if (key !== 'constructor') {
// skip first super
if (_super !== mixin && key !== 'init') {
_super[key] = previous[key];
}
if (!mixin.hasOwnProperty(key)) {
mixin[key] = previous[key];
}
}
});
// we need to go super (but not too super)
if (Object.prototype !== _prototype) {
_super._super = {};
_super = _super._super;
}
}
} while (_prototype);
this.mixin(mixin);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment