Mid JavaScript

How does this behave in different contexts?

  • Global function: this = window (browser)
  • Object method: this = the object
  • Arrow function: this = lexical context (inherits from parent scope)

Example:

const obj = {

name: "Sandeep",

arrow: () => console.log(this.name), // undefined in global

regular() { console.log(this.name); } // Sandeep

obj.regular();

obj.arrow();

More from JavaScript Tutorial

All questions for this course