arguments.slice ?

Joost Rekveld's icon

Hi,

ths probably has to do with my still sketchy knowledge of javascript,
but i don't understand why within a function this does not work:

index = addToPalette(arguments.slice(2,5));

while this does:

index = addToPalette(arguments[2], arguments[3], arguments[4]);        

the first line give me the error that arguments.slice is not a
function, which is weird, no ?
isn't the arguments property an array (so that I can call the 'slice'
method on it ??)

ciao,

Joost.

-------------------------------------------

Joost Rekveld
----------- http://www.lumen.nu/rekveld

-------------------------------------------

"The mystery of the world is the visible, not the invisible"
(Oscar Wilde)

-------------------------------------------

Joost Rekveld's icon

sorry,
the second line should be:

index = addToPalette([arguments[2], arguments[3], arguments[4]]);

(not the extra square brackets)

J.

-------------------------------------------

Joost Rekveld
----------- http://www.lumen.nu/rekveld

-------------------------------------------

"The mystery of the world is the visible, not the invisible"
(Oscar Wilde)

-------------------------------------------

Joshua Kit Clayton's icon

On Apr 20, 2006, at 4:16 PM, Joost Rekveld wrote:

> the first line give me the error that arguments.slice is not a
> function, which is weird, no ?
> isn't the arguments property an array (so that I can call the
> 'slice' method on it ??)

The arguments object is technically *not* an array, despite being
indexable as one. Note the wording "array-like" in the Core
Javascript 1.5 Guide:

http://developer.mozilla.org/en/docs/
Core_JavaScript_1.5_Guide:Using_the_arguments_object

This is why we've provided the arrayfromargs utility method which
converts an arguments object to a true array. Basically it does the
following, though from C:

function arrayfromargs(args)
{
    var myarray = new Array();
    var i;

    for (i=0;i
        myarray[i] = args[i];

    return myarray;
}

-Joshua

Joost Rekveld's icon

thanks, now I get it..

-------------------------------------------

Joost Rekveld
----------- http://www.lumen.nu/rekveld

-------------------------------------------

"The mystery of the world is the visible, not the invisible"
(Oscar Wilde)

-------------------------------------------