Skip to content

Instantly share code, notes, and snippets.

@wh1tew0lf
Created June 28, 2017 09:08
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 wh1tew0lf/efd88f57228559eab4fac29cc159d765 to your computer and use it in GitHub Desktop.
Save wh1tew0lf/efd88f57228559eab4fac29cc159d765 to your computer and use it in GitHub Desktop.
inject(mixin) {
if (Array.isArray(mixin)) {
for(let i in mixin) {
this.inject(mixin[i]);
}
return;
} else if ('object' !== typeof mixin) {
throw Error('Invalid mixin');
}
for (let prop in mixin) {
if (!mixin.hasOwnProperty(prop)) { continue; }
if ('function' === typeof mixin[prop]) {
if ('undefined' !== typeof Object.getPrototypeOf(this)[prop]) {
throw Error('Property ' + prop + ' already exists at prototype!');
}
Object.getPrototypeOf(this)[prop] = () => mixin[prop].apply(this, arguments);
} else {
if ('undefined' !== typeof this[prop]) {
throw Error('Property ' + prop + ' already exists as this!');
}
Object.defineProperty(this, 'prop', {
get: function() {
return mixin[prop];
}
});
}
}
}
use(mixin) {
if (Array.isArray(mixin)) {
for(let i in mixin) {
this.use(mixin[i]);
}
return;
} else if ('object' !== typeof mixin) {
throw Error('Invalid mixin');
}
for (let prop in mixin) {
if (!mixin.hasOwnProperty(prop)) { continue; }
if ('function' === typeof mixin[prop]) {
if ('undefined' !== typeof Object.getPrototypeOf(this)[prop]) {
throw Error('Property ' + prop + ' already exists at prototype!');
}
Object.getPrototypeOf(this)[prop] = mixin[prop];
} else {
if ('undefined' !== typeof this[prop]) {
throw Error('Property ' + prop + ' already exists as this!');
}
this[prop] = mixin[prop];
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment