0

Can we use this keyword inside the arrow function of the class?

class Student{
  constructor(name, grade){
    this.name = name;
    this.grade = grade;
  }
  need = () => {
    return this.name + " need a cricket kit";
  }
   career = () => {
    return this.need();
  }
}

const student = new Student("Raman Sharma", "7");
console.log(student.career()); //"Raman Sharma need a cricket kit"

Well, I know that: MDN says: "An arrow function does not have its own this"

 let user = { 
        name: "GFG", 
        gfg1:() => { 
            console.log("hello " + this.name); // no 'this' binding here 
        }, 
        gfg2(){        
            console.log("Welcome to " + this.name); // 'this' binding works here 
    }   
 }; 
user.gfg1(); 
user.gfg2();

I think I am misunderstanding something. Can you help me out of that?

imrvasishtha
  • 99
  • 10

0 Answers0