I'm struggling to articulate this question, but essentially, I have a class called button
that should execute the function passed to it when clicked.
button = function(...,callBack) {
//...
this._cb = callBack;
}
button.prototype.callBack = function(e) {
//...
this._cb();
}
Then elsewhere in the code
//when canvas is clicked
e.target.callBack(e);
(I hope this provides enough context, let me know if more information is required)
The issue arises when I dynamically create buttons with callbacks that rely on data from an array. For example:
for (var i = 0; i < levels.length; i++) {
buttons[buttons.length] = new button(..., function() {drawLevel(levels[i])});
}
When these buttons are clicked, they try to access 'i' which leads to unexpected behavior. My question is, how can I resolve this issue without resorting to using eval?
Thank you!