Skip to curriculum
Jump to the editor

7.5. A bind that an explicit this can still override

Hard
Topics
30–45 min

bind 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

JAVASCRIPT
fn.softBind(defaultThis, ...boundArgs) // returns a function
  • defaultThis: the this for 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 whose this is undefined, null or the global object: a plain call, a timer callback, an array callback without a thisArg, or call/apply with null, undefined or the global object.

Requirements

  1. Every function has a softBind method that does not appear in a for…in loop, with a length of 1. It returns a new function and does not call fn.
  2. A call with no real this runs fn with defaultThis (whatever it is, null or a primitive included).
  3. Any other this wins, exactly as given: an object from a method call, call, apply or an event listener, and a primitive passed with call (a strict fn sees 0 or "x" as it is, never the default). fn's own mode still applies: a sloppy fn sees a primitive wrapped in its object.
  4. boundArgs come before each call's own arguments, every call starts from the same bound arguments, and fn's result is returned.
  5. A hard bind to a real this beats a soft bind in both orders: binding the soft-bound function, or soft-binding a bound function, keeps the hard this. Binding the soft-bound function to null or undefined passes no real this, so the default is used; soft-binding a function already bound to null keeps null, because the inner hard bind ignores whatever it is given. Soft-binding a soft-bound function again gives a new default, and an explicit this still wins.
  6. With new, both the default and the caller's this are ignored: fn is constructed with the bound and given arguments, new.target inside it is fn, and the result is an instance of fn and of the soft-bound function, also when fn is itself a bound function. (When fn is a bound function, new fn() goes on to construct the function it was bound from, and JavaScript sets new.target to that original function: inside it, new.target is the original, never the bound wrapper.)
  7. The returned function's length is fn.length minus the bound arguments, never below 0.
  8. fn is not changed, separate soft binds are independent, and calling softBind on something that is not a function throws a TypeError.

Examples

JAVASCRIPT
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)
JAVASCRIPT
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"
JAVASCRIPT
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.js runs first, then the tests call softBind on functions they define in both strict and sloppy code, and use it with timers on a virtual clock, array callbacks and a real click event.
  • You may use call, apply and bind.
  • Out of scope: telling an explicit fn.call(window) apart from a plain call (both mean "no real this" here), and the returned function's name.

Hints

  1. Inside the returned function, what is this in a plain call? Does that depend on whether your file is in strict mode?
  2. In sloppy code, soft.call(5) gives the function a Number object, not 5. How can the returned function see the primitive itself?
  3. Problem 7.4 handled new with new.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
Back to the question
Loading the editor…