Last active
August 11, 2023 07:35
-
-
Save arccoza/66635b32e13459fc1cd14ad5ab7d2c79 to your computer and use it in GitHub Desktop.
JavaScript Callable Object using closures & prototypes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'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