lstd/func.ts
2025-01-07 02:58:12 +11:00

33 lines
731 B
TypeScript

// deno-lint-ignore-file no-explicit-any
export class Callable {
static {
Object.setPrototypeOf(this.prototype, Function.prototype);
}
get name() {
return this.constructor.name;
}
constructor(f: (...args: any) => any) {
let self: any;
if (typeof f.prototype !== "undefined") {
self = function call(...args: unknown[]) {
return typeof new.target !== "undefined"
? Reflect.construct(f, args, new.target)
: f(...args);
};
self.prototype = f.prototype;
} else {
self = (...args: unknown[]) => f(...args);
}
f = f.bind(self);
delete self.name;
delete self.length;
return Object.setPrototypeOf(self, new.target.prototype);
}
}