InterviewBuddy
All interview questions

How does this get its value in JavaScript?

Advanced

Answer

For ordinary functions, this is usually set by how the function is called. Arrow functions do not create their own this; they capture it lexically from the surrounding scope.

Explanation

Extracting a method can lose its original receiver. bind, call, or an arrow wrapper can preserve the intended context.

Example

const fn = object.method; fn(); // receiver is no longer object