Last active
October 1, 2017 15:44
-
-
Save jprivillaso/9d4e27e5a153d72736f286613b4f7924 to your computer and use it in GitHub Desktop.
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
// Taken from https://stackoverflow.com/questions/9163341/multiple-inheritance-prototypes-in-javascript | |
function getDesc (obj, prop) { | |
let desc = Object.getOwnPropertyDescriptor(obj, prop); | |
return desc || (obj=Object.getPrototypeOf(obj) ? getDesc(obj, prop) : void 0); | |
} | |
function multiInherit (...protos) { | |
return Object.create(new Proxy(Object.create(null), { | |
has: (target, prop) => protos.some(obj => prop in obj), | |
get (target, prop, receiver) { | |
let obj = protos.find(obj => prop in obj); | |
return obj ? Reflect.get(obj, prop, receiver) : void 0; | |
}, | |
set (target, prop, value, receiver) { | |
let obj = protos.find(obj => prop in obj); | |
return Reflect.set(obj || Object.create(null), prop, value, receiver); | |
}, | |
*enumerate (target) { yield* this.ownKeys(target); }, | |
ownKeys(target) { | |
let hash = Object.create(null); | |
for(let obj of protos) for(let p in obj) if(!hash[p]) hash[p] = true; | |
return Object.getOwnPropertyNames(hash); | |
}, | |
getOwnPropertyDescriptor(target, prop) { | |
let obj = protos.find(obj => prop in obj); | |
let desc = obj ? getDesc(obj, prop) : void 0; | |
if(desc) desc.configurable = true; | |
return desc; | |
}, | |
preventExtensions: (target) => false, | |
defineProperty: (target, prop, desc) => false, | |
})); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment