I realise JavaScript has no pointers, however I noticed this "pointer" behaviour when looping through arrays that contains objects, but not the similar behaviour when an array contains numbers (for instance).
var ARR_num = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
for (var i = 0, len = ARR_num.length; i < len; i++) {
var item = ARR_num[i];
item++;
}
console.log(ARR_num);
//Outputs [0,1,2,3,4,5,6,7,8,9]
Now with an array with objects
var ARR_obj = [{}, {}, {}];
for (var i = 0, len = ARR_obj.length; i < len; i++) {
var item = ARR_obj[i];
item.pointer = true;
}
console.log(ARR_obj);
//Outputs [{pointer: true}, {pointer: true}, {pointer: true}]
Why these two distinct behaviours?
item === ARR_obj[i].itemrefers to ARR_obj[i]item, the change made onitemaffects the original object but not the original integer