passing array to new fuction

Discussion in 'Javascript' started by windandwaves, Feb 22, 2005.

  1. windandwaves

    windandwaves Guest

    Hi Folk

    I have the following function
    var a = new Array(4);
    a[0] = new Array(45); //status (on or off)
    a[1] = new Array(45); //name of the region
    a[2] = new Array(45); //on mouse over
    a[3] = new Array(45); //on mouse out

    a[0][0] = false;
    .....snip
    a[1][0] = "region name";
    ......snip
    a[2][1] = new Array('x', 'b','c','d',); // outer islands
    a[2][2] = new Array('h', 'x', 'x', 'n');
    ....snip
    a[3][1] = new Array('a', 'b','c','d',); // outer islands
    a[3][2] = new Array('h', 'h', 'n', 'n');
    ....snip

    function Rx(i) {
    var j;
    var dd = new Array();
    document.getElementById('r'+i).checked ? j = 3 : j = 2;
    dd = a[j];
    si(dd);
    }

    function si() {
    if (document.images && (preloadFlag == true)) {
    var img;
    for (var i=0; i<si.arguments.length; i+=2) {
    img = null;
    if (document.layers) {
    img = findElement(si.arguments,0);
    }
    else {
    img = document.images[si.arguments];
    }
    if (img) {
    img.src = si.arguments[i+1]+'.gif';
    }
    }
    }
    }

    My problem is when I call the Rx(i) function, it passes the whole array (e.g. a[2][3]) as one variable to si and not an array,
    therefore, si does not work. What am I doing wrong?

    There are about 200 a[j] variables, some of which are arrays in themselves.

    TIA

    - Nicolaas
    windandwaves, Feb 22, 2005
    #1
    1. Advertising

  2. windandwaves

    RobG Guest

    windandwaves wrote:
    > Hi Folk

    [...]
    > ...snip
    > a[3][1] = new Array('a', 'b','c','d',); // outer islands


    You have a syntax error here -------^

    > a[3][2] = new Array('h', 'h', 'n', 'n');
    > ...snip
    >
    > function Rx(i) {
    > var j;
    > var dd = new Array();
    > document.getElementById('r'+i).checked ? j = 3 : j = 2;
    > dd = a[j];
    > si(dd);


    Insert:

    alert(dd);

    to see that dd is an array.

    > }
    >
    > function si() {
    > if (document.images && (preloadFlag == true)) {
    > var img;
    > for (var i=0; i<si.arguments.length; i+=2) {
    > img = null;
    > if (document.layers) {
    > img = findElement(si.arguments,0);
    > }
    > else {
    > img = document.images[si.arguments];


    Here you are grabbing the arguments, but there is only one - the
    entire array of what was formerly dd. Try:

    ...si.arguments[0]];

    [...]

    No warranty or guarantees provided.

    --
    Rob
    RobG, Feb 22, 2005
    #2
    1. Advertising

  3. windandwaves

    RobB Guest

    RobG wrote:
    > windandwaves wrote:
    > > Hi Folk

    > [...]
    > > ...snip
    > > a[3][1] = new Array('a', 'b','c','d',); // outer islands

    >
    > You have a syntax error here -------^
    >
    > > a[3][2] = new Array('h', 'h', 'n', 'n');
    > > ...snip
    > >
    > > function Rx(i) {
    > > var j;
    > > var dd = new Array();
    > > document.getElementById('r'+i).checked ? j = 3 : j = 2;
    > > dd = a[j];
    > > si(dd);

    >
    > Insert:
    >
    > alert(dd);
    >
    > to see that dd is an array.
    >
    > > }
    > >
    > > function si() {
    > > if (document.images && (preloadFlag == true)) {
    > > var img;
    > > for (var i=0; i<si.arguments.length; i+=2) {
    > > img = null;
    > > if (document.layers) {
    > > img = findElement(si.arguments,0);
    > > }
    > > else {
    > > img = document.images[si.arguments];

    >
    > Here you are grabbing the arguments, but there is only one - the
    > entire array of what was formerly dd. Try:
    >
    > ...si.arguments[0]];
    >
    > [...]
    >
    > No warranty or guarantees provided.
    >
    > --
    > Rob


    Two syntax errors.
    Not much point to this:

    function Rx(i) {
    var j;
    var dd = new Array();
    document.getElementById('r'+i).checked ? j = 3 : j = 2;
    dd = a[j];
    si(dd);
    }

    You've created a new array, assigned it to dd, and then overwritten
    that with whatever was in the element a[j]. Why not just pass the
    original reference?

    si(a[j]);

    As RobG hinted, you can't replace the arguments array (btw
    'Function.arguments' was deprecated in favor of simply 'arguments'
    around 1998) with another array passed in there; it'll be treated as
    one argument.

    function Rx(i) {
    var el;
    if (el = document.getElementById('r' + i))
    si(a[el.checked ? 3 : 2]);
    }

    function si(arr) {
    if (document.images
    && preloadFlag
    && typeof arr[0] != 'undefined') {
    var img;
    for (var i=0; i<arr.length; i+=2) {
    img = null;
    if (document.layers) {
    img = findElement(arr,0);
    }
    else {
    img = document.images[arr];
    }
    if (img) {
    img.src = arr[i+1]+'.gif';
    }
    }
    }
    }

    This sort of script is often made dramatically clearer (and more
    efficient) by replacing the nested arrays with some sort of generic
    object structure using named properties.
    RobB, Feb 22, 2005
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. mike
    Replies:
    3
    Views:
    364
    Chris Botha
    Nov 16, 2005
  2. somez72
    Replies:
    4
    Views:
    4,349
    Roedy Green
    May 3, 2004
  3. MiniDisc_2k2
    Replies:
    2
    Views:
    385
    MiniDisc_2k2
    Jul 3, 2003
  4. orion30
    Replies:
    2
    Views:
    306
    Alf P. Steinbach
    Jul 14, 2003
  5. Replies:
    6
    Views:
    106
    Evertjan.
    Oct 11, 2006
Loading...

Share This Page