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 <
[email protected]> 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 <[email protected]> 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
>> [email protected]
>> https://mail.mozilla.org/listinfo/es-discuss
>>
> _______________________________________________
> es-discuss mailing list
> [email protected]
> https://mail.mozilla.org/listinfo/es-discuss
>
_______________________________________________
es-discuss mailing list
[email protected]
https://mail.mozilla.org/listinfo/es-discuss

Reply via email to