I have a array of object, in which I need to assign unique id. To make it simple, I declared a global var for this id, that I update at every new object:
var id_unit = 0,
units = [],
merc = [
{name: 'grunt', id: 0, level: 1, hp: 1, atk: 1, def: 1, deployed: false},
{name: 'big grunt', id: 0, level: 1, hp: 1, atk: 1, def: 1, deployed: false}
];
function buy_unit(type, callback) {
var unit = merc[type];
unit.id = id_unit;//0 + id_unit //new Number(id_unit) //Number(id_unit)
id_unit = id_unit + 1;
units.push(unit);
callback('OK');
}
Problem is, when I use this function, id seems to have got the adress of unit_id instead of its value:
buy_unit
unit_id: 0
i: 0 id: 0 level: 1 deployed: false
buy_unit
unit_id: 1
i: 0 id: 1 level: 1 deployed: false
i: 1 id: 1 level: 1 deployed: false
When what I was expecting was:
buy_unit
unit_id: 1
i: 0 id: 0 level: 1 deployed: false
i: 1 id: 1 level: 1 deployed: false
Why is unit_id returning its pointer and not its value? How can I get the value?