Callable objects protocol

Andrea Giammarchi andrea.giammarchi at gmail.com
Wed Dec 5 05:54:21 UTC 2018


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
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20181205/a43b479b/attachment.html>


More information about the es-discuss mailing list