I am looking for the correct way to store references to objects in javascript.
For example I have an object customer:
function Customer(n) {
this.name = n;
}
And an array of all customers, that gets filled:
var customers = new Array()
customers.push(new Customer('Alfred'));
customers.push(new Customer('Bob'));
Now I also have several other objects which reference customers, like purchase, and outstandingOffer, promotion ect. which should all reference to elements of the customers array. For example:
function Purchase(i, c) {
this.customer = c; // ? <- this need to be a reference
this.item = i;
}
This could be done by storing the index in the array, but that seems fragile in case a customer needs to be removed. What is the best way to store a reference to another object in javascript?