Re: Proposal For A New Alternative Keyword To “this” For Classes

Bergi a.d.bergi at web.de
Mon Mar 11 19:36:57 UTC 2019


Hi John!
> I think the js run-time already has that information at hand, so as
> long as we don't implement this as pure syntactical sugar, there would
> not be a need to keep an extra reference to anything, because it would
> be already there. The run-time will know which instance the invoked
> method belongs to.

Well no, you're wrong here: the runtime does not have this information
at hand. In your example (simplified)
```
var reqManager = new RequestManager();
function addEventListener(f) {
     console.log(f);
     f(event);
}
addEventListener(reqManager.responseHandler);
```
the `addEventListener` function will not know that the function `f` you
passed was a method of the `reqManager` instance. It cannot distinguish
that call from
```
addEventListener(RequestManager.prototype.responseHandler);
```
or
```
var g = otherReqManager.responseHandler;
addEventListener(g);
```

It is exactly the same function that is passed in all three cases. There
is no instance bound to `f`, and `f(event)` will not invoke it as a
method (with a receiver/`this` value).

Best regards,
  Bergi


More information about the es-discuss mailing list