0

I am new to Javascript and trying to understand arrow functions' this in the object.

  1. const obj = {
     name:"Lia",
     sayHi: ()=> {
      return this.name
     }
    }
    
  2. const obj2  ={
        age:"40",
        nested :{
            age: 50,
            named : ()=>{
                console.log(this, "arr")
                console.log(this.age)
                let x = ()=>{
                    console.log(this, "this")
                    console.log(this.age, "Age from nest")
                }
               x()
            }
        }
    }
    

On Google I have read that the arrow functions don't bind their own scope, but inherit it from the parent one. The parent in the first example is obj object. However, when I run the code, it prints undefined, meanwhile in the second example it works fine. Why? How can I know who is the parent of the this keyword in the arrow functions?

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Diana
  • 39
  • 3

0 Answers0