Created
June 28, 2017 09:08
-
-
Save wh1tew0lf/efd88f57228559eab4fac29cc159d765 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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]; | |
} | |
}); | |
} | |
} | |
} |
This file contains hidden or 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
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