Skip to content

Instantly share code, notes, and snippets.

@arccoza
Last active August 11, 2023 07:35
Show Gist options
  • Save arccoza/66635b32e13459fc1cd14ad5ab7d2c79 to your computer and use it in GitHub Desktop.
Save arccoza/66635b32e13459fc1cd14ad5ab7d2c79 to your computer and use it in GitHub Desktop.
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)
}
_call(...args) {
console.log(this, args)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment