diff --git a/dist/index.js b/dist/index.js index 9f36155..3c0e466 100644 --- a/dist/index.js +++ b/dist/index.js @@ -24,6 +24,13 @@ function getMixables(clientKeys, mixin) { return descriptors; function getMixables(obj) { var map = {}; + var base = Object.getPrototypeOf(obj); + if (base !== Object.prototype) { + var baseDescriptors = getMixables(base) || {}; + for (var i in baseDescriptors) { + map[i] = baseDescriptors[i]; + } + } Object.getOwnPropertyNames(obj).map(function (key) { if (clientKeys.indexOf(key) < 0) { var descriptor = Object.getOwnPropertyDescriptor(obj, key); diff --git a/src/index.ts b/src/index.ts index 77374a4..25d776d 100644 --- a/src/index.ts +++ b/src/index.ts @@ -2,18 +2,18 @@ export type Constructor = new (...args: any[]) => T; export type Mixin = Constructor | object; function mix(client: Constructor, mixins: Mixin[]) { - const clientKeys = Object.getOwnPropertyNames( client.prototype ); + const clientKeys = Object.getOwnPropertyNames(client.prototype); for (let mixin of mixins) { const mixinMixables = getMixables(clientKeys, mixin); - Object.defineProperties( client.prototype, mixinMixables ); + Object.defineProperties(client.prototype, mixinMixables); } } /** * Returns a map of mixables. That is things that can be mixed in */ -function getMixables(clientKeys:string[], mixin: Mixin) { - let descriptors:PropertyDescriptorMap = {}; +function getMixables(clientKeys: string[], mixin: Mixin) { + let descriptors: PropertyDescriptorMap = {}; switch (typeof mixin) { case "object": descriptors = getMixables(mixin); @@ -24,24 +24,31 @@ function getMixables(clientKeys:string[], mixin: Mixin) { } return descriptors; - function getMixables(obj:object):PropertyDescriptorMap { - const map:PropertyDescriptorMap = {}; - Object.getOwnPropertyNames( obj ).map( key => { - if( clientKeys.indexOf( key ) < 0 ) { - const descriptor = Object.getOwnPropertyDescriptor( obj, key ); - if( descriptor === undefined ) return - if( descriptor.get || descriptor.set ) { + function getMixables(obj: object):PropertyDescriptorMap { + const map: PropertyDescriptorMap = {}; + const base = Object.getPrototypeOf(obj); + if (base !== Object.prototype) { + var baseDescriptors = getMixables(base) || {}; + for (var i in baseDescriptors) { + map[i] = baseDescriptors[i]; + } + } + Object.getOwnPropertyNames(obj).map(key => { + if(clientKeys.indexOf(key) < 0) { + const descriptor = Object.getOwnPropertyDescriptor(obj, key); + if(descriptor === undefined) return + if(descriptor.get || descriptor.set) { map[ key ] = descriptor; } else - if ( typeof descriptor.value === "function" ) { + if (typeof descriptor.value === "function") { map[ key ] = descriptor; } } }) return map; } - + } /** @@ -55,10 +62,10 @@ export function use(...options: Mixin[]) { } /** - * Takes a method as a parameter and add it to the class calling it. + * Takes a method as a parameter and add it to the class calling it. */ export function delegate(method: (...args: any[]) => any) { return function (target: any, propertyKey: string) { target.constructor.prototype[propertyKey] = method; } -} \ No newline at end of file +}