Facebook Pixel
this Keyword Interview Questions in JavaScript

What happens to this when a method is detached from its object in JavaScript?

The answer

this is lost. Calling const fn = obj.method; fn() is a plain call, so this is the global object or undefined, not obj. Fix with .bind(obj) or an arrow wrapper.

More FAQs in this Keyword Interview Questions in JavaScript

In non-strict mode, this is the global object (window). In strict mode, this is undefined. This is default binding, the lowest-priority rule.

An arrow inside a constructor inherits this from the constructor's scope, where this is the new instance. This is why arrows fix the setTimeout-in-constructor bug.

new binding (highest) > explicit binding (bind/call/apply) > implicit binding (method call) > default binding (plain call). Arrow functions are an exception: they use lexical this and ignore all of these.

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.

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

Success

Please Login.

Success

Please Login.

Success

Please Login.

Success

Please Login.

Success

Please Login.

Success

Please Login.

Success

Please Login.

Success

Please Login.

Success

Please Login.

Success

Please Login.

Success

Please Login.

Success

Please Login.

Success

Please Login.

Success

Please Login.

Success

Please Login.

Success

Please Login.

Success

Please Login.

Success

Please Login.

Success

Please Login.

Success

Please Login.