Last active
August 29, 2015 14:14
-
-
Save bloodyowl/a07ff6a9f171db7374b9 to your computer and use it in GitHub Desktop.
mixins
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
var emptyFunction = () => {} | |
/** | |
* creates a new constructor function with `objects` merged in its prototype. | |
* to prevent unwanted errors when calling `super()` a child class, the | |
* prototype is actually a proxy returning **or** the actual method, or an | |
* empty function. | |
* | |
* as the proxy gets the same shape as the initial object, the prototype chain | |
* actually receives the present methods. | |
* | |
* @param {Object} ...objects | |
* @returns {Function} | |
*/ | |
function mixins(...objects) { | |
var proto = {} | |
objects.forEach((object) => { | |
if(object == null || object === false) { | |
return | |
} | |
for(let methodName in object) { | |
let previousMethod = proto[methodName] | |
let method = object[methodName] | |
let actualMethod = method | |
if(previousMethod) { | |
// if mixins have multiple methods, we merge them | |
actualMethod = function(...args){ | |
previousMethod.apply(this, args) | |
method.apply(this, args) | |
} | |
} | |
proto[methodName] = actualMethod | |
} | |
}) | |
function ComposedClass(){ | |
ComposedClass.prototype.constructor.apply(this, arguments) | |
} | |
ComposedClass.prototype = new Proxy(proto, { | |
get(object, methodName) { | |
return proto[methodName] || emptyFunction | |
}, | |
set() { | |
throw new TypeError() | |
} | |
}) | |
return ComposedClass | |
} | |
const A = { | |
constructor() { | |
this.name = "defaultName" | |
}, | |
getName() { | |
return this.name | |
}, | |
setName(name) { | |
this.name = name | |
} | |
} | |
const B = { | |
constructor() { | |
this.foo = "bar" | |
} | |
} | |
class Test extends mixins(A, B) { | |
constructor() { | |
super() | |
} | |
test() { | |
super() // doesn't throw, as the proxy returns an empty function | |
return "foo" | |
} | |
} | |
const t = new Test() | |
console.log(t.name) | |
console.log(t.foo) | |
console.log(t.test()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
a little idea with mixins implementing the class mixin idea from @sebmarkbage with
the help of ES6 proxies to prevent
super()
to throw if no method in the prototype chain is matchingthis call.