Well this is pretty basic, but I couldn't find the answers alone. So I hope you guys can help me.
I was trying to bind an event to an array of object, but somehow the variable that got passed is the last one. After it got iterated.
for (var i = 0; i < map_object.length; i++) {
map_object[i].on('mousedown', function(){
var x = i;
setEvent(this, x);
});
};
function setEvent(data, i){
console.log(data);
var x = i;
console.log(x);
}
the data got passed by value so it always return dynamically, meanwhile (i variable) got passed as reference.
So when it executed I always get this result
data = (some object) {expected result}
i = 9 {unexpected} = map_object.length
I can't figure if this is because (i) got passed as reference, or its because the live event is executed after iteration completed.
the on function come from Fabricjs