freecoding.school100% FREE · NO SIGNUP
Closure CavesISSUE #10 of 61

call · apply · bind · changing this

LambdaVSThe Mutator
Lambda sayscall, apply, and bind set this explicitly; bind returns a new permanently-bound function.

fn.call(thisArg, a, b) invokes with a chosen this and individual args; fn.apply(thisArg, [a, b]) takes an args array. fn.bind(thisArg) doesn't call — it returns a new function with this locked in, ideal for passing methods as callbacks without losing context.

Power-ups you unlock

The Mutator attacks — common mistakes

Boss battleBorrow a method with call by setting this to another object.

Example code

<!doctype html><html><head><meta charset="utf-8"></head>
<body style="background:#06040d;color:#e6e0ff;font-family:monospace;padding:20px"><pre id="o"></pre>
<script>function intro(){ return 'I am '+this.name; }
document.getElementById('o').textContent=intro.call({name:'borrowed this'});</script></body></html>
▶ Open the interactive comic issue
‹ This · What It Points ToArguments · The Legacy Array-Like ›