I am learning the behavior of this
keyword in ES6 class , before I have learnt that this
keyword in object functions, represent the following :
- In regular function,
this
represents the the current object. - In arrow function,
this
represent the global object(window).
So, when I worked with ES6 class I see that Class behave as Object
when it is complied into JavaScript ,I see this behavior in console by putting this
in a class method and this
was representing the current class as Object
as well as its prototype was Object
.
For conceptual understanding, I make one Arrow function foo()
and one Regular function bar()
in a class AppComponent and print this
keyword as follow:
I was expecting that this
in foo() would represent global object(window) , because it is inside Arrow function in the Object
and ,on the other side,in the bar() this
represent the current object due to Regular function inside object
which is AppComponent, but the output was:
bar() output is expected.
foo() output is unexpected.
So, my question is that why this
represent the current Object Appcomnent rather than global object(window), because it is inside the arrow function
in the object .
Please correct me if my concept is not correct in the question ,if it is correct then why I see the unexpected output. I would very thankful to you for your contribution.