Callable objects protocol
Isiah Meadows
isiahmeadows at gmail.com
Tue Dec 4 19:14:05 UTC 2018
BTW, there are proxies [1], and one of the proxy hooks is to intercept
calls [2].
[1]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy
[2]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy/handler/apply
Your "callable object" proposal would be literally as simple as this
to implement:
```js
const callable = Symbol.for("callable")
const handler = {
apply(target, thisArg, argsList) {
return Reflect.apply(target[callable], thisArg, argsList)
},
}
function makeCallable(obj) { return new Proxy(obj, handler) }
// Your example, ported
const obj = makeCallable({
[callable]: function (...args) { return this[Symbol.for('value')] },
[Symbol.for(''value')]: 'value',
})
assert(obj() === 'value')
obj[callable] = () => 1
assert(obj() === 1)
```
-----
Isiah Meadows
contact at isiahmeadows.com
www.isiahmeadows.com
On Tue, Dec 4, 2018 at 12:02 PM Sultan <thysultan at gmail.com> wrote:
>
> Something along the lines of Symbol.iterator protocol for defining callback objects i.e: Symbol.callable:
>
> const obj = {
> [Symbol.callable]: function (...args) { return this[Symbol.for('value')] },
> [Symbol.for(''value')]: 'value',
> }
>
> assert(obj() === 'value')
> _______________________________________________
> es-discuss mailing list
> es-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss
More information about the es-discuss
mailing list