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?
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:
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:
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:
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:
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:
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.