Skip to content

Instantly share code, notes, and snippets.

@arccoza
Last active March 20, 2024 15:12
Show Gist options
  • Save arccoza/50fe61c8430fc97a463bf6b8960776ce to your computer and use it in GitHub Desktop.
Save arccoza/50fe61c8430fc97a463bf6b8960776ce to your computer and use it in GitHub Desktop.
JavaScript Callable Object using proxy
'use strict'
class Callable extends Function {
constructor() {
super()
return new Proxy(this, {
apply: (target, thisArg, args) => target._call(...args)
})
}
_call(...args) {
console.log(this, args)
}
}
@jed
Copy link

jed commented Dec 29, 2023

you could also reuse the object itself!

class Callable extends Function {
  constructor() {
    super()    
    return new Proxy(this, this)
  }
  
  apply(...args) {
    console.log(this, args)
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment