call, apply and bind — choosing this yourself¶
Level: 201 · for anyone who has seen .bind(this) and wondered what it fixes
Stub — an outline, not a lesson. There is no runnable example behind this page yet, so nothing on it has been through the check that backs every other claim in this library. The questions below are what the finished page has to answer.
One line: call and apply choose this for one call; bind returns a new function whose this no later call or bind can change — yet new on a bound function still builds a fresh object and ignores the bound this.
Keywords: call, apply, bind
What the finished page will answer¶
- What is the one difference between
callandapply, and what doesf.apply(obj, args)do thatf(...args)cannot? - What does a bound function return when you
callit with anotherthis, orbindit a second time? - Which arguments does
bindfix in advance, and what are the bound function'snameandlength? - What happens to the bound
this, and to the bound arguments, when a bound function is called withnew? - What does
typeof thisprint afterf.call(5)in a module, and in a sloppy script?
Examples it will need¶
-
call_apply_bind_compared_js.js— one function run through call, apply and bind, then the bound version called with another this, bound again, and constructed with new -
call_apply_bind_primitive_this_sh.sh— typeof this after f.call(5) and this after f.call(null), run as a module and as a sloppy .cjs script -
call_apply_bind_checked_tserror.ts— tsc's errors for a missing argument to call (TS2554), a wrong type in apply's array (TS2322) and a wrong type passed to a bound function (TS2345)
See also¶
this— the default these three methods override- Arrow functions — the functions whose this none of the three can change
new— why new ignores a bound thisstrict— strictBindCallApply, the flag that type-checks these calls- Rust:
implblocks ↗ — how Rust treats a method: an associated function whose first parameter is self