Facebook Pixel

Why should object methods not be arrow functions in JavaScript?

Because an arrow function's this is the enclosing scope's this, not the object. So obj.greet = () => this.name gives undefined. Use regular functions for methods so this is the object.

Verify This Answer

Cross-check this information using these trusted sources:

More FAQs in this in Arrow Functions vs Regular Functions in JavaScript

Regular functions get this at call time based on how they are invoked. Arrow functions do not have their own this; they inherit it from their enclosing lexical scope at definition time.

Because arrows inherit this from their lexical scope. An arrow inside a method or constructor captures this from that scope, so it still refers to the instance inside the callback.

No. Arrow functions do not have their own this binding. bind, call, and apply cannot change an arrow's this. They can still pass arguments, but this stays lexical.

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.
Please Login.