Callable objects protocol
Logan Smyth
loganfsmyth at gmail.com
Tue Dec 4 19:13:28 UTC 2018
The type of the target is what affects typeof, indirectly by setting the
[[Call]] value. If an `apply` or `construct` trap handler is given and the
target isn't a function, they are ignored. See step 7 in
https://www.ecma-international.org/ecma-262/9.0/index.html#sec-proxycreate
On Tue, Dec 4, 2018 at 2:08 PM Isiah Meadows <isiahmeadows at gmail.com> wrote:
> BTW, `typeof new Proxy({}, {apply() { return 1 }})` returns
> `"object"`, not `"function"`. Not sure this is any different.
>
> -----
>
> Isiah Meadows
> contact at isiahmeadows.com
> www.isiahmeadows.com
> On Tue, Dec 4, 2018 at 2:02 PM Jordan Harband <ljharb at gmail.com> wrote:
> >
> > What would `typeof` return on a callable object? There's a lot of code
> on the web that uses a typeof of "function" to determine if something is
> safe to call. There's also a lot of code on the web that uses a typeof of
> "function" to do type detection. How would you suggest a callable object
> navigate these potentially conflicting constraints?
> >
> > On Tue, Dec 4, 2018 at 10:07 AM T.J. Crowder <
> tj.crowder at farsightsoftware.com> wrote:
> >>
> >> On Tue, Dec 4, 2018 at 5:02 PM Sultan
> >> <thysultan at gmail.com> wrote:
> >> >
> >> > Something along the lines of Symbol.iterator protocol for defining
> >> > callback objects i.e: Symbol.callable:
> >>
> >> That's interesting. What's the primary motivation? Binding the data?
> That's easily done with a closure ([fiddle[1]):
> >>
> >> ```js
> >> const obj = (() => {
> >> let value = 'value';
> >> return () => value;
> >> })();
> >>
> >> console.log(obj() === 'value'); // true
> >> ```
> >>
> >> It's a bit more verbose than one would like, but usually that's washed
> out by the implementation (which is really trivial above).
> >>
> >> You can also do it with a Proxy on a function, but it doesn't really
> buy you anything.
> >>
> >> Be interested to know more about why you want non-function callable
> objects.
> >>
> >> -- T.J. Crowder
> >>
> >> [1]: http://jsfiddle.net/8qvpc7Lh/
> >> _______________________________________________
> >> 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
> _______________________________________________
> 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/0c5f3dd8/attachment.html>
More information about the es-discuss
mailing list