Cannot use a method in a button's callback

Hello,
I have a button in my class. The button’s callback is supposed to call a method of the class buttonCB(), but I am getting an error on click
Uncaught TypeError: this.buttonCB is not a function

However, if my callback is a function, not a class’ method, I am getting the expected results

Can you please let me know what I am missing in the first example?

It might be related to my other post

thank you

This is because ‘this’ has changed; you should use arrow functions to avoid ‘this’ reference errors.
pg: https://playground.babylonjs.com/#DZQ7MD#2

4 Likes

@musk Thank you for your solution and highlighting usage of arrow functions. That is super useful as I am new to javascript

1 Like