You can use an anonymous function like this:
// it will show "undefined" because "this" scope changes to window
funWithCallBack(testClass.show);
to this:
// anonymous function to use the right object for the method
funWithCallBack(function() {
testClass.show()
});
Your problem occurs because when you pass testClass.show
as the callback, it just gets the function reference and there is no association with testClass
any more. But, you can create a temporary function that binds them together using .bind(), but it isn't supported in some older browsers which is why I generally just use the anonymous function.
The .bind()
implementation would look like this:
// use .bind() to make sure the method is called in the right object context
funWithCallBack(testClass.show.bind(testClass));