Skip to content

Instantly share code, notes, and snippets.

@arccoza
arccoza / index.js
Last active October 7, 2024 16:51
JavaScript Callable Object using proxy
'use strict'
class Callable extends Function {
constructor() {
super()
return new Proxy(this, {
apply: (target, thisArg, args) => target._call(...args)
})
}
@arccoza
arccoza / index.js
Last active August 11, 2023 07:35
JavaScript Callable Object using closures & prototypes
'use strict'
class Callable extends Function {
constructor() {
var closure = function(...args) { return closure._call(...args) }
// Or without the spread/rest operator:
// var closure = function() {
// return closure._call.apply(closure, arguments)
// }
return Object.setPrototypeOf(closure, new.target.prototype)