I have a library the needs an array of buttons in order to produce a UI, this array has objects with a Text to show and a callback to call when the button is pressed. For this example as callback I'm using a console.log to show a value that gets randomly generated during the population of the array:
var buttons = [];
for(var x = 0; x < 3; x++) {
var value = Math.random()*10;
buttons.push({
text: "Foo",
onTap: function() { console.log("Im a button and my value is", value) } })
}
As you can see right away since onTap gets called later on when the user taps the button, the value that console.log shows is the last one, meaning that all buttons will show the same value. I can modify the library to include some kind of meta field in the object where I can store the values and then retrieve them from the onTap function. But I prefer not to touch the library, is there a better way to tackle this?