As stated in this blog post and in this stack overflow post, it is possible to make an object callable as follows:
class Callable extends Function {
constructor() {
super();
var closure = function (...args) { return closure._call(...args) }
return Object.setPrototypeOf(closure, new.target.prototype)
}
_call(...args) {
console.log(this, args)
}
}
However, this causes problems with CSP (Content-Security-Policy), like when running in a Chrome extension. It is possible to remove the extends Function
and super()
calls, but this results in type errors. For my use-case, I unfortunately can’t ignore this. If there’s a way to override the type errors (e.g., using JSDoc), that could also work.
Any help will be greatly appreciated.