Callable objects protocol

Ranando King kingmph at gmail.com
Tue Dec 4 22:15:05 UTC 2018


Thinking again, this might be a request for static lexical scope variables
such that:

```js
function obj() {
  static value = { test: 42 };
  return obj.value;
}

var a = obj();
assert(obj() === a);
```

On Tue, Dec 4, 2018 at 4:05 PM Ranando King <kingmph at gmail.com> wrote:

> Ok.... maybe I'm thinking a little to literally, but isn't a function
> already a callable object?
>
> ```js
> function obj() {
>   return obj.value;
> }
> obj.value = "value";
>
> assert(obj() === "value");
> ```
>
> On Tue, Dec 4, 2018 at 1:16 PM Isiah Meadows <isiahmeadows at gmail.com>
> wrote:
>
>> Edit: the wrapper needs to be a function, so ignore that last email. It's
>> wrong.
>>
>> -----
>>
>> Isiah Meadows
>> contact at isiahmeadows.com
>> www.isiahmeadows.com
>>
>> On Tue, Dec 4, 2018 at 2:14 PM Isiah Meadows <isiahmeadows at gmail.com>
>> wrote:
>> >
>> > 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
>> _______________________________________________
>> 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/20181204/d8f36d97/attachment-0001.html>


More information about the es-discuss mailing list