‘this’ binding in class in javascript [duplicate]

I have doubt in below code:

class A{

    a(){
        this.name='bhavuk'
        let x=()=> console.log(this)   //Here this should point to global coz of arrow function
        x()
    }
}
let temp=new A()
temp.a()      // output {name: 'bhavuk'}  

I know arrow functions have lexical scoping, So I think 'this' should bind to global object. Please correct me if I make any mistake



Read more here: https://stackoverflow.com/questions/64406167/this-binding-in-class-in-javascript

Content Attribution

This content was originally published by BHAVUK GARG at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: