Callable objects protocol

Ranando King kingmph at gmail.com
Wed Dec 5 06:25:12 UTC 2018


Maybe I asked it wrong.

How is making an ordinary object callable at all useful for anything that
can't already be easily handled via objects and functions? (looking for use
cases here)
How does this make coding easier to do and understand? (for the AST parser
and for the human)

On Tue, Dec 4, 2018 at 11:54 PM Andrea Giammarchi <
andrea.giammarchi at gmail.com> wrote:

> How about this:
>
> ```js
>
> // the poly
> if (!Symbol.callable)
>   Symbol.callable = Symbol('callable');
>
> // the setup
> class Callable extends Function {
>   constructor(object) {
>     super('return arguments.callee[Symbol.callable](...arguments)');
>     //            sloppy mode FTW!
>     Object.setPrototypeOf(this, object);
>   }
> }
>
>
> // the example
> const obj = new Callable({
>   [Symbol.callable](value) {
>     return value + this.value;
>   },
>   value: 123
> });
>
> obj(7); // 130
>
>
> ```
>
> On Wed, Dec 5, 2018 at 12:02 AM 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
>>
> _______________________________________________
> es-discuss mailing list
> es-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20181205/1b982bf7/attachment-0001.html>


More information about the es-discuss mailing list