When a function is called as a method (obj.method()), this is the object to the left of the dot at call time. The object that owns the method call determines this.
What is implicit binding of this in JavaScript?
More FAQs in Default and Implicit this Binding in JavaScript
When a function is called as a plain function (not as a method), this is the global object in non-strict mode, or undefined in strict mode. This is the lowest-priority binding.
Because the method is detached from obj when passed as a reference. setTimeout calls it as a plain function, not as a method of obj. this becomes global or undefined. Fix with .bind(obj) or an arrow wrapper.
Use .bind(obj) to permanently bind this, or wrap the method call in an arrow function (() => obj.method()), or define the method as an arrow in the object. Arrows inherit this lexically.
Still have questions?
Browse all our FAQs or reach out to our support team
Want to upskill yourself?
Your learning library
Small steps. Big possibilities.
Find your next course. Build something great.