Loading video player…

Saving a Receiver and Arguments With bind()

Saving a Receiver and Arguments With bind()

In this lesson, you meet .bind() and use it to hand the desk’s formatter on. The desk’s format method turns pence into a price in the desk’s currency.

In Python, reading desk.format saves a bound method: the desk comes with it, and the later call prints GBP 48.00. In JavaScript, the same line keeps only the function: call it on its own, and its this is undefined. .call() gives the desk back, but it runs the formatter right now. The pipeline wants to hand the formatter to .map(). An arrow such as (pence) => desk.format(pence) already works, and functions also have a built-in tool for this job.

.call() and .apply() both call the function now, and .bind() takes the receiver the same way. So what does each one make?

Language: JavaScript
const desk = {
  currency: "GBP",
  format(pence) {
    const amount = (pence / 100).toFixed(2);
    return `${this.currency} ${amount}`;
  },
};
const detached = desk.format;
const viaCall = detached.call(desk, 4800);
const viaBind = detached.bind(desk);
console.log(typeof viaCall, typeof viaBind);
console.log(viaBind === detached);

Run with node desk.mjs, typeof reports a string, then a function:

Language: Program Output
string function
false

.call() ran the formatter and returned its text. .bind() ran nothing: it returned a new function. Python draws the same line: Desk.format(desk, 4800) runs now and returns a str, and desk.format gives a bound method.

Here the analogy stops. Python saves the desk every time you read the method through it. JavaScript finds this at each call, and saves a receiver only when you ask with .bind(). On functions, === asks “same function?”, like Python’s is. .bind() made a new one, so the comparison logs false, and detached itself still has no receiver.

So viaCall holds a result, like a call with parentheses. viaBind is like Python’s desk.format, no parentheses: a function still to call.

To hand a formatter on, bind straight off the method and name the result. Reading desk.format brings no desk, so you name it again inside .bind(). .map() needs something it can call, so pass the bound function, never a call’s result:

Language: JavaScript
const desk = {
  currency: "GBP",
  format(pence) {
    const amount = (pence / 100).toFixed(2);
    return `${this.currency} ${amount}`;
  },
};
const formatPrice = desk.format.bind(desk);
console.log(formatPrice(4800));
console.log([4800, 4800].map(formatPrice));

The guide’s quote and its repeat each get the desk’s currency, as Python’s map() does with the bound method:

Language: Program Output
GBP 48.00
[ 'GBP 48.00', 'GBP 48.00' ]

The bound function and last lesson’s arrow are both common: an arrow reads better when its call needs more than a receiver. The receiver is fixed for good, as a Python bound method keeps its desk. A later .call() with another object, or storing the bound function on another object, doesn’t change it.

The guide stays at 400 words while the desk tries rates, so fix the 400 once. In Python, functools.partial(price, 400) fixes it as the first argument, and the later call with 12 pence a word prints 4800. In JavaScript, .bind() takes the receiver first, in self’s slot, then any leading arguments. price reads no this, so the receiver is undefined, on purpose:

Language: JavaScript
function price(words, ratePence) {
  return words * ratePence;
}
const priceForGuide = price.bind(undefined, 400);
console.log(priceForGuide(12));
const priceAt12 = (words) => price(words, 12);
console.log(priceAt12(400));

The 400 fills words and the later 12 fills ratePence, and price itself stays as it was. Both lines log 4800:

Language: Program Output
4800
4800

JavaScript has no keyword arguments, so .bind() can only fill parameters from the left. Python’s partial can preset rate_pence=12 by keyword and leave words open; in JavaScript, a named arrow passes the 12 itself. So .bind() covers one job of partial, the leading arguments, and isn’t a replacement for functools. For a later argument, the arrow says what’s fixed.

So .call() and .apply() call now and return the result. .bind() calls nothing: it returns a new function, with its receiver and any leading arguments saved, and the original stays as it was. For a later argument, use a named arrow.

Next up is the text lesson Choosing Between an Arrow and a Method. There, you’ll see why an arrow has no receiver of its own at call time, and why an object literal doesn’t give this a scope.

00:00 In this lesson, you’ll meet .bind() and use it to hand the formatter on. The desk’s format method turns pence into a price in its currency. In Python, reading desk.format saves a bound method: the desk comes with it, and the later call prints GBP 48.00.

00:19 In JavaScript, the same line keeps only the function: call detached on its own, and its this is undefined. .call() gives the desk back and logs GBP 48.00, but it runs the formatter now.

00:35 The pipeline wants to hand it to .map(). An arrow such as (pence) => desk.format(pence) already works. Functions also have a built-in tool for this job.

00:48 .call() and .apply() both call the function now. Their sibling, .bind(), takes the receiver the same way. So try both: one line passes the desk and 4800 to .call(), and the next passes the desk to .bind().

01:04 Then typeof reports what each one made. What does typeof log for each result? Your turn: pause the video and predict.

01:19 It logs string, then function.

01:23 .call() ran the formatter and returned its text. .bind() ran nothing: it returned a new function.

01:32 Python draws the same line: calling Desk.format with the desk as self runs it now and returns a string, like .call().

01:41 Reading desk.format gives a bound method, like .bind(). Python prints str and method.

01:50 Here the analogy stops. Python saves the desk every time you read the method through it. JavaScript finds this at each call, and saves a receiver only when you ask with .bind().

02:02 On functions, triple equals asks “same function?”, like Python’s is. .bind() made a new one, so the comparison logs false.

02:12 detached still has no receiver. So viaCall holds a result, like a call with parentheses. viaBind is like Python’s desk.format, no parentheses: a function still to call.

02:27 To hand a formatter on, bind off the method and name the result. Reading desk.format brings no desk, so name it again inside .bind().

02:37 Call formatPrice later, and it logs GBP 48.00. Hand it to .map() for the guide’s quote and its repeat: each gets the desk’s currency, as in Python.

02:50 Pass the function, never a call’s result: .map() needs something to call. The bound function and last lesson’s arrow are both common: an arrow reads better when its call needs more than a receiver.

03:03 The receiver is fixed for good, as a Python bound method keeps its desk. A later .call(), or storing it on another object, doesn’t change it.

03:14 The guide stays at 400 words while the desk tries rates, so fix the 400 once. In Python, functools.partial fixes the 400 as the first argument of price, and the later call with 12 pence a word prints 4800.

03:32 In JavaScript, .bind() takes the receiver first, in self’s slot, then any leading arguments. price reads no this, so the receiver is undefined, on purpose.

03:45 The 400 fills words, and the later 12 fills ratePence: JavaScript logs 4800. And price itself stays as it was.

03:57 JavaScript has no keyword arguments, so .bind() can only fill parameters from the left. Python’s partial can preset rate_pence by keyword and leave words open.

04:09 In JavaScript, write a named arrow that passes the 12 itself. Both print 4800 for 400 words. So .bind() covers one job of partial, the leading arguments, and isn’t a replacement for functools.

04:25 For a later argument, the arrow says what’s fixed.

04:31 So .call() and .apply() call now and return the result. .bind() calls nothing: it returns a new function, with its receiver and any leading arguments saved, and the original stays as it was.

04:44 For a later argument, use a named arrow. Next up is the text lesson Choosing Between an Arrow and a Method. There, you’ll see why an arrow has no receiver of its own at call time, and why an object literal doesn’t give this a scope.

Become a Member to join the conversation.