JavaScriptIntermediate#this#functions

How does 'this' work in JavaScript?

In a normal function 'this' is decided by the call site: the object before the dot, undefined in strict mode standalone calls, the new instance with new, or an explicit value with call/apply/bind. Arrow functions capture 'this' lexically instead.

Example
const obj = {
  name: 'A',
  regular() { return this.name; },
  arrow: () => this?.name,
};
obj.regular(); // 'A'
obj.arrow();   // undefined

Related Questions

1
JavaScriptIntermediate#functions#this

Difference between call, apply and bind?

Open
2
JavaScriptIntermediate#functions#es6

Difference between arrow functions and regular functions?

Open
3
JavaScriptIntermediate#prototypes#oop

What is prototypal inheritance?

Open