@segment/analytics-core
Version:
This package represents core 'shared' functionality that is shared by analytics packages. This is not designed to be used directly, but internal to analytics-node and analytics-browser.
20 lines (18 loc) • 504 B
text/typescript
export function bindAll<
ObjType extends { [key: string]: any },
KeyType extends keyof ObjType
>(obj: ObjType): ObjType {
const proto = obj.constructor.prototype
for (const key of Object.getOwnPropertyNames(proto)) {
if (key !== 'constructor') {
const desc = Object.getOwnPropertyDescriptor(
obj.constructor.prototype,
key
)
if (!!desc && typeof desc.value === 'function') {
obj[key as KeyType] = obj[key].bind(obj)
}
}
}
return obj
}