0

How to use this key word to retrieve the functions property.

myfunction = () =>{
  console.log(this.age);//undefied expecting 30
}

myfunction.age = 30;

myfunction();

In the above, this refer the window. how can we bind the this value to myfunction?

user2024080
  • 5,068
  • 12
  • 46
  • 81
  • 1
    You cannot. `this` cannot be altered after creating an arrow function. Moreover, `this` does not refer to the function object itself like you seem to expect it. It refers to the context where the function was executed. E.g., `foo.fn()` will execute `fn` with `this = foo`. While `foo.bar.fn()` will execute with `this = foo.bar`. Unless overridden. And same stipulation for arrow functions remains. [How does the "this" keyword work?](https://stackoverflow.com/q/3127429) – VLAZ Feb 21 '22 at 06:36

0 Answers0