1

Is it possible to have a object constructor where each objects has its own array?

I am have trouble figuring out the syntax if so.

Right now I am creating objects and a separate array to co-aside the object(waiters) array.

function Waiter (name, orders[]) {
    this.name = name;
    this.orders = orders;
}

// Constructor for waiter object
function Waiter (name) {
    this.name = name;
}

// Waiter objects
var waiterOne = new Waiter('Timo');
var waiterTwo = new Waiter('Lucian');
var waiterThree = new Waiter('Arpi');


// Array to store waiter object 
var waiters = [
    waiterOne,
    waiterTwo,
    waiterThree
];

// Count so that the same number of arrays are create as waiters
var countWaiterOrders = waiters.length;

// Creating a order array for each waiter object
for(var i = 0; i <= countWaiterOrders; i++){
    var order = [i];
}

Getting error:

Uncaught SyntaxError: Unexpected token [

Is the error message I get when trying to pass an array to the constructor.

The desired result would just be that each Waiter object has its own array for orders.

ex:

console.log(waiters[0]);

Waiter {name: "Timo", orders: []}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Liam
  • 568
  • 2
  • 15
  • 1
    Change `order[]` to `order` in the method definition here `function Waiter (name, order)`. – Tigger Aug 08 '19 at 10:36

2 Answers2

1

Silly question I was just a bit stuck for a while you assign the value to an empty array not the argument.

//Waiter constructor

function Waiter (name, order) {
    this.name = name;
    this.order = [];
}
Liam
  • 568
  • 2
  • 15
1

Please correct me if I'm missing something, but it seems that you could just use something like this:

class Waiter {
      constructor(name) {
        this.name = name
        this.orders = []
      }
    }

What you are doing here is creating a class Waiter to which you pass a name as variable.

You can create a Waiter like so: var waiterOne = new Waiter('Tim').

This would then allow you to use waiterOne.name or waiterOne.orders, as the array of orders is created in the constructor of the class.

If you wanted to store all your waiters in the array, the good method may be to create a collection class called Waiters - This could be useful if you wanted to do some operations on the whole collection of your Waiters.

Irek
  • 187
  • 2
  • 12
  • 1
    The current syntax will allow you to use `waiterOne.name` as well. – Tigger Aug 08 '19 at 10:39
  • 1
    Yes, I'm aware. I just wanted to mention this, so it's clear that he can still use `.name` on the object. – Irek Aug 08 '19 at 10:45
  • Thanks @Tigger, Irek So if later I want to be pushing orders to the individual waiters.order.Array. Would it be better to use a class or an Object? – Liam Aug 08 '19 at 11:40
  • @Liam : Suggest having a read of [Object vs Class vs Function](https://stackoverflow.com/questions/17525450/object-vs-class-vs-function) - Please read the comments too - and decide what is best for you. – Tigger Aug 08 '19 at 22:26