0

Why arrow function this return window object and how I return obj ?

let obj = {
  name:"Maxi",
  fn  :()=>this,
}

console.log(obj.fn())
Murad Sofiyev
  • 730
  • 5
  • 21

2 Answers2

2

Why arrow function this return window object

Because arrow functions capture the current value of this at the time they are created.

how I return obj

Don't use an arrow function if you don't want to capture the current value of this.

Quentin
  • 800,325
  • 104
  • 1,079
  • 1,205
1

With arrow functions, this is not bound to its own context.

Arrow functions do not create their own this context, so this has its original meaning from the enclosing context which is window in your case.

Sebastian Simon
  • 14,320
  • 6
  • 42
  • 61
Abhinav Galodha
  • 7,820
  • 2
  • 27
  • 37