0

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?

2 Answers 2

1

looking at below you approach is different

var customers = new Array()
customers.push(new Customer('Alfred'));
customers.push(new Customer('Bob'));

You are pushing new objects in an array without saving a reference to it.So your purchase function will never know what is who or who is what

This is How I would approach it

function Customer(n) {
  this.name = n;
  this.items=[];
  this.addPurchase=function(item){
  this.items.push(item);
  }
}

The above function will have the follow

  1. The name of the customer
  2. A function that adds an item to the customer item cart
  3. An item cart
var customers = {}; //create a big object that stores all customers
customers.Alfred=new Customer('Alfred'); // create a new object named Alfred
customers.Bob=new Customer('Bob'); // create a new object named Bob
customers.John=new Customer('John'); // create a new object named John

Using console.log, you will get

Alfred: Object, Bob: Object, John: Object

If you want to add items to Alfred you do this

customers.Alfred.addPurchase('pineapple');

If you want to add items to Bob you do this

customers.Bob.addPurchase('mango');

If you want to add items to John you do this

customers.John.addPurchase('coconut');

This is output from console.log(customers.John.items);

Array [ "coconut" ]

So what if we want to delete a customer? We already have a reference to it!

delete customers.John;

John and this history is gone!...Verify it is deleted

console.log(customers);

output

Object { Alfred: Object, Bob: Object }
Sign up to request clarification or add additional context in comments.

Comments

0

use new to create object

var customers = new Array()
customers.push(new Customer('Alfred'));
customers.push(new Customer('Bob'));

function Purchase(i, c) {
  this.customer = c; // ? <- this need to be a reference
  this.item = i; 
}

var Purchase_obj = new Purchase(2,customers[0] );

2 Comments

if I change the name of the customer, will that show up in the purchase? what happens when a customer in customers gets deleted?
@Beginner if you update customer name that will reflect in purchase object, that is the main concept of oop , if you remove one customer from customers array it will not be destroyed, you can still get customer name from purchase object

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.