tc39 / proposal-partial-application

Proposal to add partial application to ECMAScript
https://tc39.es/proposal-partial-application/
BSD 3-Clause "New" or "Revised" License
1.02k stars 25 forks source link

method extraction #28

Closed ljharb closed 5 years ago

ljharb commented 6 years ago

Please feel free to point me at the right place if this is a duplicate.

I find it very important that const x = a.b(?, d); delete a.b; a = null; x(c) work - ie, that the receiver and the function are eagerly preserved at the moment of evaluating the partial application.

This allows for method extraction, such that I can do something like:

const slice = Function.call(Array.prototype.slice, ?, ?);

/* normal web code, ie untrusted code, runs, and might mess with `Array.prototype` */

slice(anyArray, x, y); // works
rbuckton commented 6 years ago

I would intend for this to work, and will update the explained accordingly when time permits.

rbuckton commented 5 years ago

This example is illustrated in https://github.com/tc39/proposal-partial-application#semantics:

In addition to fixing the function to be called and its explicit arguments, we also fix any supplied receiver as part of the resulting function, as we will store the Reference in the resulting function. As such, o.f(?) will maintain o as the this receiver when calling o.f. This can be illustrated by the following syntactic conversion:

const g = o.f(?, 1);

is roughly identical in its behavior to:

const g = (() => {
  const receiver = o;
  const fn = o.f;
  const p0 = 1;
  return (a0) => fn.call(receiver, a0, p0);
})();