Anybody on #TC39 want to champion an ECMAScript proposal? Before I write a whole explainer document I’d like to make sure it isn’t wasted effort.
In short: Symbol.call, a well-known symbol, which can be used as a method name to make an object callable. Prior art: #Ruby’s call(), E’s run (), C++’s operator(). Allows your class’s instance to act as a Functor Object, and eases use of Method Object pattern. Gives us a way to make callables have their own “this.”
@kerrick you can return a function from the constructor, that will make the instance creates with new callable. But it's a bit difficult to get all the properties set correctly on it.
@gundersen @kerrick Could Proxy it. (Not saying it's a _good_ idea. ) Roughly:
@gundersen @kerrick Doh! No need for the `apply` trap (went down that path because I was going to return a plain object, but you can't use the apply trap on a non-callable):
@kerrick This was suggested in the es6 era. It met resistance from implementers because they do not like when things are unpredictable.
You should be able to find the meeting notes online.
Other problems occur due to how `typeof` works for any object that has `[[Call]]`. Should it change based on the symbol?
I'm not saying it is not possible to achieve this but there are a lot of hard problems here.
@kerrick You can ask if TC39 is interested here: https://es.discourse.group
Implementation via a helper class: https://gist.github.com/rauschma/99c4c15259671acb47b651e0d1da17aa