arrows and a proposed softCall

Andreas Rossberg rossberg at google.com
Mon Jun 4 08:16:45 PDT 2012


On 3 June 2012 20:13, Angus Croll <anguscroll at gmail.com> wrote:
> Not suggesting that arrow functions be special cased or that they ignore
> |thisArg|. I'm suggesting for all relevant cases (=>, bind and no |this|) we
> only throw an error on call/apply if the |thisArg| is non null.

That would not only be a breaking change, as Brendan pointed out. More
importantly, it would also not be something you truly want. Consider
this piece of code:

  function C(x) { this.x = x }
  C.prototype.f = function() { return this.x }
  C.prototype.g = function() { return this.f() * 2 }
  C.prototype.h = function() { return 41 }

  let o = new C(21)
  foo(o, [o.f, o.g, o.h])

where

  function foo(o, funs) {
    // ...
    for (let f of values(funs)) {
      result += f.call(o)
    }
  }

According to your suggestion, foo would throw. But there is no actual
reason why foo should care that h is a constant method. That's an
implementation detail. Why should the caller have to jump through
hoops to account for implementation details of the callee?

I think there is a misconception here is that this-oblivious
methods/functions are something _different_ from "normal"
methods/functions. They aren't. They are just a _special case_ of
normal methods/functions. Just like functions that ignore any other
parameter. Consequently, you don't usually want to treat them
differently. I claim that code that tries has a 98% likelihood of
being broken -- or at least of smelling rather bad.

/Andreas


More information about the es-discuss mailing list