JavaScript wart: this-binding in setTimeout and setInterval

By Xah Lee. Date: . Last updated: .

This-binding broke in settimeout and setinterval

a JavaScript wart.

// 2026-05-04
// calling object.method() does not get thisbinding correctly
// when in setTimeout(object.method(), delay)
// why?

const xobj = {
 f_print_thisbinding() {
  console.log(this);
 },
};

setTimeout(xobj.f_print_thisbinding, 1000);
// Window {}

JavaScript really sucks.

Why this binding does not work in settimeout

(ai answer)

ai js thisbinding setTimeout 2026-05-04 1701e
ai js thisbinding setTimeout 2026-05-04 1701e