Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Sep 2011
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    What does 'push' do?

    Hi..

    I have some code I wrote a while ago and now I can't remember what it does!

    Code:
    var subArray = [];
    subArray[0] = frontEndOptionsId;
    subArray[1] = document.getElementById(frontEndOptionsId).value;		
    BYOT5.push(subArray);
    And further down I have...

    Code:
    xh.sendRequest("GET","/byot5.php",BYOT5,callback,false);
    Am I right in guessing that the 'push' pushes the elements in subArray in to BYOT5 in my second segment? Would it look like...

    Code:
    xh.sendRequest("GET","/byot5.php",subArray("ZERO","ONE"),callback,false);
    ...when it's sent to byot5.php? Or am I completely wrong?!
    Last edited by Mike1985; 10-13-2011 at 04:48 PM. Reason: grammer/spelling

  • #2
    Regular Coder
    Join Date
    Mar 2008
    Location
    London
    Posts
    152
    Thanks
    4
    Thanked 42 Times in 42 Posts
    Push and Pop methods are associated with Array object, to represent the functionality of STACK.

    in your case, BYOT5.push(subArray) represents a two-dimensional array, something like shown below -
    [
    [frontEndOptionsId, document.getElementById(frontEndOptionsId).value]
    ];

  • #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,084
    Thanks
    80
    Thanked 4,552 Times in 4,516 Posts
    Mike:

    subArray("ZERO","ONE") is meaningless.

    It might be clearer to say that this:
    Code:
    xh.sendRequest("GET","/byot5.php",BYOT5,callback,false)
    is the same as this:
    Code:
    xh.sendRequest( 
        "GET",
        "/byot5.php",
        [ 
            [ frontEndOptionsId, document.getElementById(frontEndOptionsId).value ]
        ],
        callback,
        false)
    In other words, BYOT is an array of arrays. It happens that the outer array only has one element (unless you didn't show us all the push() calls?) and the inner array that is that one element in turn has two elements.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,084
    Thanks
    80
    Thanked 4,552 Times in 4,516 Posts
    And as to what actually happens: You don't show us the code for sendRequest( ) but assuming you are making an AJAX call, then the *actual* call to PHP will be done something like this, almost surely:

    Let's say that frontEndOptionsId is the string "zamboni".

    And somewhere in your HTML you have something like
    Code:
          <input id="zamboni" />
    and the user types in "froozle" to that text field.

    So, then, you will end up doing
    Code:
    xh.sendRequest( 
        "GET",
        "/byot5.php",
        [ 
            [ "zamboni", "froozle" ]
        ],
        callback,
        false)
    and I'll bet a small latte that sendRequest will end up using the url:
    Code:
        /byot5.php?zamboni=froozle
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •