transferring the "this" value to a function

Hey, i have a class with a function in that logs out a this. variable. When i call that function inside of a function outside of the class it works, however when i call that function as a callback, it can't find the this value and i have to use .bind (i'm sure call and apply could potentially be used too) to allow it to work, since the callback parameter has the function as the value, i don't understand what causes that error compared to straight up calling it. i'd appreciate any help, thanks in advance. https://codepen.io/deerCabin/pen/RNbQvbd
4 Replies
Ganesh
Ganesh2w ago
whenever you pass a function to another function as a callback you pass it by referance. This causes it to lose it's this value since it lost the context
Ganesh
Ganesh2w ago
No description
Ganesh
Ganesh2w ago
from mdn
snxxwyy
snxxwyyOP2w ago
Ah okay I see

Did you find this page helpful?