this: how the call sets it
this is an implicit parameter that the call supplies. It is not the object that holds the function, and it is not fixed when the function is written: the same function may see one this in one line and another in the next, because the two calls are written differently. Four forms decide it, and they are the four sections of this lesson.
The rule is short enough to memorise. A call of the form object.method() sets this to the object before the dot, a plain call leaves it undefined in strict code, an arrow function has no this of its own, and call, apply and bind set it by hand. The programs below run each form; a module is strict, so the plain call reports the error it really produces.
A method call
When a function is reached through an object and called with parentheses after it, this is that object. The function has no name of its own in the object: the same function value can be a method of two objects, and each call sees the object before the dot.
const user = {
name: 'Ada',
greet() {
return `Hello, ${this.name}`;
},
};
console.log(user.greet());
const other = { name: 'Grace', greet: user.greet };
console.log(other.greet());
const detached = user.greet;
try {
detached();
} catch (error) {
console.log(error.name + ': ' + error.message);
}
node method.mjs
Hello, Ada
Hello, Grace
TypeError: Cannot read properties of undefined (reading 'name')
The second line is the proof that the object is not part of the function: the same greet printed a different name because a different object stood before the dot. The third line is the same function called without an object, which the next section takes apart.
A plain call
detached() has no object before the dot, so there is nothing to use as this. In strict code, and a module is strict, this is undefined, and reading a property of it throws the TypeError the program printed. A callback handed to another function is the same case: the caller decides how the function is called, not the place where it was written.
A file without strict mode does something worse: this becomes the global object, so the read of name finds whatever the global object has under that name, or undefined. That is why the two modes are worth knowing before the next lesson, and why a method that is passed around is usually wrapped in an arrow or bound first.
Arrow functions keep the outer this
An arrow function has no this of its own. When it reads this it reads the one from the place it was written, and that never changes, however the arrow is called. The program below returns an arrow from a method: the arrow keeps the this of the method call, and still works after the object is left behind.
const user = {
name: 'Ada',
greetLater() {
const greet = () => `Hello, ${this.name}`;
return greet;
},
};
const detached = user.greetLater();
console.log(detached());
console.log('arrow at the top level:', typeof (() => this)());
node arrow.mjs
Hello, Ada
arrow at the top level: undefined
The detached arrow printed the name of the object, because the method call gave the environment its this and the arrow kept it. The second line is the top level of a module, where there is no object and this is undefined: an arrow written there keeps that value for ever. In a browser script the top level is the window object instead, which is the difference the strict mode lesson describes.
call, apply and bind
Every function object has three methods that set this by hand. call(thisArg, ...args) runs the function now with the arguments given; apply(thisArg, argsArray) does the same with the arguments in an array; bind(thisArg, ...args) returns a new function that is fixed to that object and can be called later.
function greet(greeting) {
return `${greeting}, ${this.name}`;
}
const ada = { name: 'Ada' };
const grace = { name: 'Grace' };
console.log(greet.call(ada, 'Hello'));
console.log(greet.apply(grace, ['Hi']));
const helloAda = greet.bind(ada, 'Hello');
console.log(helloAda());
const user = { name: 'Ada', greet };
const detached = user.greet;
console.log(detached.bind(user)('Hello'));
node bind.mjs
Hello, Ada
Hi, Grace
Hello, Ada
Hello, Ada
The four lines print the same shape of answer because each call supplied the object. The last one is the fix for the detached method of the first program: bind produced a function that already knows its this, so a later plain call is safe. The bound argument stays fixed as well, which is why helloAda() needs no greeting.
Exercises
- Add a second method to the object in method.mjs that calls this.greet() and returns the result. Which object does the inner call see?
- Replace detached() in method.mjs with detached.call(user). Which line of the output disappears?
- Change the arrow in arrow.mjs to function () { return `Hello, ${this.name}`; } and run it. What does the detached call print now, and why?
- Bind greet in bind.mjs to an empty object and print the result of the call. What does this.name give?
- Write a function that takes a callback and calls it, then pass a method of an object as that callback. Explain the result with the word call.
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
What decides the value of
thisinside a function?Answer
How the function is called
this is an implicit parameter supplied by the call, which is why the same function can see different objects.
-
What does
thisbecome in a plain call of an ordinary function inside a module?Answer
undefined
A module is strict code, and strict mode leaves this undefined when the call supplies no object.
-
What does this program print? ``
const user = { name: "Ada", greet() { returnHello, ${this.name}; }, }; console.log(user.greet());``Answer
Hello, Ada
The method call set this to the object before the dot, so this.name is the name field.
-
What does this program print?
const user = { name: "Ada", greet() { return this.name; } }; const other = { name: "Grace", greet: user.greet }; console.log(other.greet());Answer
Grace
The call is made through other, so this is other even though the function was written in user.
-
What does this module print?
function show() { return typeof this; } console.log(show());Answer
undefined
The plain call supplies no object and the module is strict, so this is undefined and typeof reports its type.
-
What does an arrow function use as
this?Answer
The this of the place where it was written
An arrow has no this binding of its own, so it reads the one from the enclosing scope.
-
What does this program print?
const user = { name: "Ada", greetLater() { return () => this.name; }, }; const fn = user.greetLater(); console.log(fn());Answer
Ada
The arrow kept the this of the method call, so it still reads the name of the object after the call has returned.
-
What does
bindreturn?Answer
A new function fixed to the object and arguments given
bind builds a new function and leaves the call for later, while call and apply run the function immediately.
-
What does this program print?
function greet() { return this.name; } const bound = greet.bind({ name: "Ada" }); console.log(bound());Answer
Ada
The bound function calls greet with the object as this, so this.name is Ada.
-
Why does a method that is passed as a callback often lose its object?
Answer
Because the caller makes a plain call, which supplies no this
The function value carries no object, so the this of the original method call is not part of the call the callback makes.
The code on this page was run with Node.js 24.22.1 and linted with ESLint 10.14.0 on Alpine Linux 3.24.3 (x86_64) in a Docker container.
Article author: Arthur Isaev