tags:

views:

55

answers:

2

Is it possible to create multiple instances of an object in javascript such that they all are ready to be manipulated/edited at the same time? This relates to my earlier question here: Structure of orders in restaurant. What I am trying to do is keep each order object ready for editing until the customer is ready to pay/leave so that new items can be added to or existing items removed from them as desired - and it has to be possible for all order objects at the same time.

If the number of tables isn't very big - say, about 15 - would it be better to create a static array of 15 objects with different table numbers?

+3  A: 

Er, yes - trivially (rough code warning):

// your class function
function MyClass(params)
{
  this.foo = params.foo;
  this.bar = params.bar;
  // etc...
}

// object or array to maintain dynamic list o instances
var instances = [];

// create instances in storage object
instances.push(new MyClass({foo:123, bar:456}));
instances.push(new MyClass({foo:'abc', bar:'def'}));

// or alternately by key
instances['mykey'] = new Myclass({foo:'argle',bar'bargle'});

Don't create a static array because there's just no need when a dynamic structure is trivial enough. Perhaps I'm missing something from your question?


Edit: update with more illustrative code based on your earlier question, yet another way to solve the problem.

At this point however this is kind of a teaching thing only. If this was real application I would advise you to model all of this in a server side language - JS is really for controlling UI behaviour not business object modelling.

var Restaurant = {

    Order : function (params)
       {
        this.id = params.id;
        this.table = params.table;
        this.items = [];
        this.number_of_items = 0;

        if(!Restaurant.Order.prototype.addItem)
        {
         Restaurant.Order.prototype.addItem = function (item)
         {
          // assuming name is unique let's use this for an associative key
          this.items[item.name] = item;

          this.number_of_items++;

          //returning the item let's you chain methods
          return item;
         }
        }
       },

    Item : function (params)
       {
        this.name = params.name;
        this.quantity = params.quantity;
        this.unit_price = params.unit_price;

        if(!Restaurant.Item.prototype.price)
        {
         Restaurant.Item.prototype.price = function ()
         {
          return this.quantity * this.unit_price;
         }
        }
       },

    orders : [],

    addOrder : function (order)
       {
        // assuming id is unique let's use this for an associative key
        this.orders[order.id] = order;
        //returning the item let's you chain methods
        return order;
       }
}

with (Restaurant)
{
    with (addOrder( new Restaurant.Order({id:123, table:456}) )) // chaining!
    {
     addItem( new Restaurant.Item({name: 'foo', quantity: 10, unit_price: 10}) );
     addItem( new Restaurant.Item({name: 'bar', quantity: 100, unit_price: 1}) );
    }
}

var num_items = Restaurant.orders[123].items['foo'].price(); // returns 100
annakata
You got it right to the point. Jazaakallaahu khairun. I'm just very new to both javascript and oop. So it's still difficult for me to grasp simple matters.
Mussnoon
No worries - when I say trivial that's not a reflection on you, that's a reflection on the problem. CMS has given you yet another way to do this (factory method) based on your original question, so you see with JS there are always many way to achieve a given solution.
annakata
"model all of this in a server side language - JS is really for controlling UI behaviour not business object modelling."*ahem* you are assuming JS is not serverside :) http://en.wikipedia.org/wiki/Server-side_JavaScript
oberhamsi
A fairly safe assumption :P
annakata
+1  A: 

Since you are using object literals on your previous question, I suggest you to give a look to this prototypal inheritance technique, it will allow you to easily create new object instances inheriting from a base instance, for example:

// Helper function
if (typeof Object.create !== 'function') {
    Object.create = function (o) {
        function F() {}
        F.prototype = o;
        return new F();
    };
}

// "Base" order object
var order = {
    id: 0,
    table: 0,
    items: []
};

var orders = [], n = 10;
while (n--) {  // make 10 objects inheriting from order and add them to an array
  orders.push(Object.create(order)); 
}

Later on, you can access and manipulate your order objects in the orders array:

orders[0].id = 10;
orders[0].table = 5;
orders[0].items.push({
  name: "Beer",
  quantity: 1,
  unit_price: 3
});
CMS