Understanding Array Object's? baskets[baskets.length]=o;

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • divina11
    New Member
    • Aug 2007
    • 55

    Understanding Array Object's? baskets[baskets.length]=o;

    A Simple program, all I'm trying to do is input item and quantity into a text box and then display them, I have written the following code, however I'm unclear what the following line does: baskets[baskets.length]=o;

    Can someone explain to me what this line actually does? Thank you


    See below for my coding:

    [code=javascript]

    var baskets=[];

    function doAction() {

    var item=document.g etElementById(" item").value;
    var quantity=docume nt.getElementBy Id("quantity"). value;

    var o=new basket(item,qua ntity);
    baskets[baskets.length]=o;
    ShowItem_All();
    }

    function basket(item,qua ntity) {

    this.item=item;
    this.quantity=q uantity;
    }[/code]
  • pbmods
    Recognized Expert Expert
    • Apr 2007
    • 5821

    #2
    Heya, Divina.

    Please use CODE tags when posting source code. See the REPLY GUIDELINES on the right side of the page next time you post.

    Comment

    • gits
      Recognized Expert Moderator Expert
      • May 2007
      • 5388

      #3
      hi ...

      i comment the code:


      [code=javascript]

      // declare an array for storing your items
      var baskets = [];

      function doAction() {
      // retrieve the inputvalues
      var item = document.getEle mentById("item" ).value;
      var quantity = document.getEle mentById("quant ity").value;

      // create an instance of the below defined obj basket
      // it calls the constructor with two params that are
      // instantly assigned as member variables to the obj-
      // instance
      var o = new basket(item,qua ntity);

      // add the instance to the array at the last index
      // it is the same as baskets.push(o) ;
      baskets[baskets.length] = o;

      // some function call
      ShowItem_All();
      }

      // this is the constructor (quasi-class) for a basket-obj
      // it receives 2 params that are stored in the obj-instance
      function basket(item, quantity) {
      this.item = item;
      this.quantity = quantity;
      }
      [/code]

      kind regards

      Comment

      • divina11
        New Member
        • Aug 2007
        • 55

        #4
        Thank you Gits, that was a very clear explaination.

        Ta XXX

        Comment

        • gits
          Recognized Expert Moderator Expert
          • May 2007
          • 5388

          #5
          hi ...

          no problem ... glad to be able to help you ... and whenever you have more questions then post in the forum again

          kind regards

          Comment

          Working...