7.5. A bind that an explicit this can still override
Topics
This question is in
7. this, call, apply and bindbind fixes this for good: once a method is bound, nothing can point it at another object. That is often too strong. A widget library wants its methods to keep their widget when passed to setTimeout or forEach, where they would lose this, but still let otherWidget.render() or render.call(otherWidget) choose a different one. The answer is a soft bind: a default this that is used only when a call gives no real this of its own.
Add softBind to every function, in soft-bind.js. fn.softBind(defaultThis, ...boundArgs) returns a function that uses defaultThis when it is called with no real this, and the caller's this otherwise.
Signature
fn.softBind(defaultThis, ...boundArgs) // returns a functiondefaultThis: thethisfor calls that give none.boundArgs: arguments placed before the ones each call passes.- Returns a new function. A call with "no real
this" is one whosethisisundefined,nullor the global object: a plain call, a timer callback, an array callback without athisArg, orcall/applywithnull,undefinedor the global object.
Requirements
- Every function has a
softBindmethod that does not appear in afor…inloop, with alengthof 1. It returns a new function and does not callfn. - A call with no real
thisrunsfnwithdefaultThis(whatever it is,nullor a primitive included). - Any other
thiswins, exactly as given: an object from a method call,call,applyor an event listener, and a primitive passed withcall(a strictfnsees0or"x"as it is, never the default).fn's own mode still applies: a sloppyfnsees a primitive wrapped in its object. boundArgscome before each call's own arguments, every call starts from the same bound arguments, andfn's result is returned.- A hard
bindto a realthisbeats a soft bind in both orders: binding the soft-bound function, or soft-binding a bound function, keeps the hardthis. Binding the soft-bound function tonullorundefinedpasses no realthis, so the default is used; soft-binding a function already bound tonullkeepsnull, because the inner hard bind ignores whatever it is given. Soft-binding a soft-bound function again gives a new default, and an explicitthisstill wins. - With
new, both the default and the caller'sthisare ignored:fnis constructed with the bound and given arguments,new.targetinside it isfn, and the result is an instance offnand of the soft-bound function, also whenfnis itself a bound function. (Whenfnis a bound function,new fn()goes on to construct the function it was bound from, and JavaScript setsnew.targetto that original function: inside it,new.targetis the original, never the bound wrapper.) - The returned function's
lengthisfn.lengthminus the bound arguments, never below 0. fnis not changed, separate soft binds are independent, and callingsoftBindon something that is not a function throws aTypeError.
Examples
function render(label) {
return label + ": " + this.id;
}
const widget = { id: "main" };
const other = { id: "side" };
const show = render.softBind(widget, "Widget");
show(); // "Widget: main" (plain call: the default)
show.call(other); // "Widget: side" (explicit this wins)
other.show = show;
other.show(); // "Widget: side" (method call wins)
setTimeout(show, 0); // "Widget: main" (a timer gives no real this)function strictSelf() { "use strict"; return this; }
const soft = strictSelf.softBind("default");
soft(); // "default"
soft.call(0); // 0: a real this, even though it is falsy
soft.call(null); // "default"const hard = render.bind(other).softBind(widget);
hard("x"); // "x: side": the hard bind wins
render.softBind(widget).bind(other)("y"); // "y: side"Environment
- Plain JavaScript in a browser page;
soft-bind.jsruns first, then the tests callsoftBindon functions they define in both strict and sloppy code, and use it with timers on a virtual clock, array callbacks and a realclickevent. - You may use
call,applyandbind. - Out of scope: telling an explicit
fn.call(window)apart from a plain call (both mean "no realthis" here), and the returned function'sname.
Hints
- Inside the returned function, what is
thisin a plain call? Does that depend on whether your file is in strict mode? - In sloppy code,
soft.call(5)gives the function aNumberobject, not5. How can the returned function see the primitive itself? - Problem 7.4 handled
newwithnew.target. Does a soft bind need anything different?
The explanation comes with the course
A walkthrough of every line of a solution, the mistakes the tests catch, other ways to write it, and links to the lessons that teach each idea.
- Every line explained
- The mistakes each test catches
- Links to the lessons behind it