← Back to Node.js, page 10

nodejs-0186 · Easy fresher

Assume this file runs as a CommonJS script in sloppy mode (no 'use strict'). ```js const student = { name: 'Asha', regular() { return this.name; }, arrow: () => this === module.exports, later() { setTimeout(function () { console.log('4', this === student, typeof this); }, 0); setTimeout(() => console.log('5', this.name), 0); }, }; const detached = student.regular; console.log('1', student.regular()); console.log('2', student.arrow()); console.log('3', detached()); student.later(); console.log('6', detached.call({ name: 'Ravi' }), detached.bind({ name: 'Meera' }).call({ name: 'Other' })); ``` What does it print, and why?

The synchronous lines print 1 Asha, 2 true, 3 undefined and 6 Ravi Meera. Then the timers print 4 false object and 5 Asha. A method called as obj.method() gets obj as this. An arrow function takes this from where it was written (the module, whose this is module.exports). A detached method has no object, so this is globalThis in sloppy mode. call and bind set this, and bind wins over a later call. A normal function used as a timer callback gets the Timeout object as this.

Full answer

Line by line:

  • Log 1: student.regular() is called on student, so this is student and it returns Asha.
  • Log 2: arrow is an arrow function. It has no this of its own and uses the this of the module scope, which in a CommonJS file is module.exports. So this === module.exports is true.
  • Log 3: detached is the same function without the object. The call detached() has no receiver. In sloppy mode this becomes globalThis, and globalThis.name is undefined, so the result is undefined. In strict mode this would be undefined and the line would throw a TypeError.
  • student.later() registers two timers and returns.
  • Log 6: detached.call sets this to the given object and returns Ravi. detached.bind({ name: 'Meera' }) creates a new function with this fixed to Meera. Calling it with call({ name: 'Other' }) cannot change it, so it returns Meera.
  • All synchronous lines are done, so the timers fire.
  • Log 4: a function expression passed to setTimeout is called by Node with this set to the Timeout object, not to student. So this === student is false and typeof this is object.
  • Log 5: the arrow function inside later() uses the this of later(), which is student, so it prints Asha.

Order: 1, 2, 3, 6 from the synchronous code, then 4 and 5 from the two timers (same delay, so in the order they were created).

Rules: this depends on how a function is called, except arrow functions, where it depends on where they are written.

javascript
const student = {
  name: 'Asha',
  regular() { return this.name; },
  arrow: () => this === module.exports,
  later() {
    setTimeout(function () { console.log('4', this === student, typeof this); }, 0);
    setTimeout(() => console.log('5', this.name), 0);
  },
};
const detached = student.regular;
console.log('1', student.regular());
console.log('2', student.arrow());
console.log('3', detached());
student.later();
console.log('6', detached.call({ name: 'Ravi' }), detached.bind({ name: 'Meera' }).call({ name: 'Other' }));

The snippet is the puzzle itself. The output is the order of the log lines. Save it as app.js and run it with node app.js. Output (Node.js v24.22.1, run once): 1 Asha 2 true 3 undefined 6 Ravi Meera 4 false object 5 Asha

Key points

  • A method call obj.f() sets this to obj.
  • A detached method loses this (globalThis in sloppy mode, undefined in strict mode).
  • Arrow functions use the this of the surrounding scope.
  • bind fixes this permanently; call and apply set it for one call.
  • A timer callback written as a normal function gets the Timeout object.

Common mistakes

  • Thinking that student.arrow() uses student as this.
  • Expecting detached() to throw in sloppy mode.
  • Believing that call can override a bound function.
  • Expecting this inside setTimeout to be the object that registered it.
  • Putting the timer lines before the synchronous lines.

Follow-ups

  • What changes if the file starts with 'use strict'?
  • What is this at the top level of an ES module?
  • How do class methods lose this when passed as callbacks, and how do you fix it?

Tips

  • List the four binding rules.
  • Mention arrow functions separately.
  • Say what strict mode changes.