I'm asking for the most common way to use a class member as callback as shown here: http://jsfiddle.net/3fupbut9/7/
// function that calls the callback
function call(cb) {
cb();
}
// class containing the callback
class UnderTest {
constructor() {
this.number = 5;
}
// method to be called as a callback by someone else
printNumber() {
alert(this.number);
}
// current solution
printNumberCB() {
return function() {
this.printNumber();
}.bind(this);
}
}
var underTest = new UnderTest(); // creat a new instance
underTest.number = 7;
underTest.printNumber(); // print number directly
call(underTest.printNumberCB()); // print number by callback
I am a C++ dev and very new to JavaScript and currently play around with nodejs and express. I think I understand how this
works in JS but what is the most common way using ES6 classes.
My real example is an express router callback.