Callable objects protocol
Andrea Giammarchi
andrea.giammarchi at gmail.com
Wed Dec 5 06:33:23 UTC 2018
I've actually replied to the op, I didn't mean to answer you directly, but
the only reason I wrote that is because I could, no other reasons.
However, people unaware of the handleEvent pattern for event listeners
often hope to be able to pass objects as listeners, ignoring the fact they
can do that already (but they need a handleEvent method, own or inherited,
in that object).
There is at least another use case I can't remember now, but I do remember
doing the Proxy dance before ending up realizing that the apply hook needs
objects anyway.
But yeah, I don't think it's a must have, specially because we can have
something similar already, as shown in my example.
On Wed, Dec 5, 2018 at 1:25 PM Ranando King <kingmph at gmail.com> wrote:
> 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/28725c34/attachment.html>
More information about the es-discuss
mailing list