Facebook Pixel

In nested objects, which object determines this in JavaScript?

Only the last object before the method call. For outer.inner.greet(), this is inner (the immediate object), not outer. The object to the left of the dot at call time is this.

Verify This Answer

Cross-check this information using these trusted sources:

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.

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.

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.

Still have questions?

Browse all our FAQs or reach out to our support team

Want to upskill yourself?

Our courses are taking a Coffee break, but your curiosity shouldn't. Stay engaged with namastedev linkedin, youtube, discord and other resources while you wait.

0
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.