Finding this at the Call Site

The translation desk’s Python script uses a method to turn integer pence into a line such as GBP 48.00 at the end of every quote. The JavaScript service needs that formatter too, and later a wrapper will call it on the desk’s behalf. First, you need to know how a JavaScript function learns which object it’s working for.

In Python, the answer is settled when you look the method up. In JavaScript, it’s settled each time you call the function. In this lesson, you’ll identify the receiver in each call and then choose it yourself.

Give the Desk a Method

The Python script keeps the currency on a Desk class, and format() reads it through self. Looking the method up on an instance gives you a bound method, which carries its instance, so you can save it and call it later:

Language: Python
class Desk:
    currency = "GBP"

    def format(self, pence):
        return f"{self.currency} {pence / 100:.2f}"

desk = Desk()
saved = desk.format
print(saved(4800))  # GBP 48.00

The JavaScript service needs only one desk, so an object literal can hold the method directly, with no class. Inside a method, this names the object the method works for, like Python’s self, except that you never list it as a parameter:

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

The price stays in integer pence and is divided only for display:

Language: Program Output
GBP 48.00

The format(pence) { ... } entry is method shorthand: a property whose value is a regular function, written the way you’d write a method in a class.

Find the Receiver Before the Dot

Python’s bound method carries desk with it. A JavaScript function doesn’t. It sits in a property, and any object can hold it. Suppose a partner desk in Lyon wants the same formatting in euros:

Language: JavaScript
const desk = {
  currency: "GBP",
  format(pence) {
    return `${this.currency} ${(pence / 100).toFixed(2)}`;
  },
};
const euroDesk = { currency: "EUR", format: desk.format };
console.log(euroDesk.format(4800));

The borrowed function reads the euro desk’s currency:

Language: Program Output
EUR 48.00

As with class methods, a regular function’s this is the object the call reads the function from, the one before the dot at the call site. In euroDesk.format(4800), that object is euroDesk, so this.currency is "EUR". In Python, copying desk.format onto another object would still print GBP 48.00, because the bound method captured desk when you looked it up.

Watch out: unlike Python’s saved = desk.format, saving or passing along a JavaScript method gives you the bare function, with no memory of desk. So if detached holds desk.format, detached(4800) throws a TypeError about reading currency of undefined, because the call has no receiver. To pass the method along, use an arrow function such as (pence) => desk.format(pence), which keeps desk at the call site.

Older tutorials say a plain call gives this the global object. That doesn’t happen in the desk’s service, which uses ECMAScript (ES) modules because its package.json sets "type": "module". An .mjs file is an ES module too. Modules always run in strict mode, where a plain call leaves this as undefined. At the top level of a module, this is also undefined:

Language: JavaScript
console.log(this);

In a module file, it logs undefined, not a global object:

Language: Program Output
undefined

Choose the Receiver With .call() and .apply()

A wrapper holds a bare function and has to call it for the right object. In Python, you’d look the function up on the class and pass the instance as self yourself:

Language: Python
print(Desk.format(desk, 4800))  # GBP 48.00

JavaScript’s version is .call(), which every function has. For a regular function like format(), the first argument to .call() becomes this for that call, and the rest go to the function. Its sibling .apply() takes the receiver, then the arguments in one array:

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

Both lines run the formatter immediately, with desk as the receiver:

Language: Program Output
GBP 48.00
GBP 48.00

The two methods differ only in how the arguments arrive. Use .call() for arguments you write out, like Desk.format(desk, 4800). Use .apply() for arguments already in an array, much like Python’s Desk.format(desk, *args) with a list. That array often comes from a rest parameter like (...args), which gathers a call’s arguments.

Spreading the array into .call() works equally well: detached.call(desk, ...args) gives the same result. Like Python’s *args, spread unpacks any iterable, not only an array.

Python’s self is an ordinary first parameter, but this is a separate input that each call fills in. To find a regular function’s this, read the call. It’s the object the call reads the function from, the first argument to .call() or .apply(), or undefined for a plain call in a module. Neither method keeps the receiver afterward. Each calls the function now and returns its result.

Next up is Saving a Receiver and Arguments With bind(), where you’ll make a formatter that keeps desk for later calls and lock in a price function’s leading word count so that you need to supply only the rate.

Become a Member to join the conversation.