0

I have this code:

const foo = function() {
  console.log(this) // foo {}

  function bla() {
    console.log(this) // Window {}
  }
  bla()
}

let a = new foo()

Why does the first console.log(this) returns the foo object but the second one inside the function returns the Window object if the foo function is used as a constructor? Shouldn't both console.log(this) return the foo object?

Laaouatni Anas
  • 4,199
  • 2
  • 7
  • 26
  • 1
    Take a look at [this community wiki answer](https://stackoverflow.com/a/3127440/979052), it explains what you're seeing very clearly. – Alicia Sykes Sep 10 '22 at 11:57

0 Answers0