Page 1 of 1

eval() can't make function arguments?

Posted: Wed Jul 04, 2007 1:29 pm
by superdezign
Here's a simple test for something I'd like to do:

Code: Select all

function Test(a, b, c)
{
    alert('Length: ' + arguments.length + ', arguments[1]: ' + arguments[1]);
}
Test(1, eval('2, 3'));
This outputs:

Code: Select all

Length: 2, arguments[1]: 3
Any idea why, and how I can get something like this to work?

Posted: Wed Jul 04, 2007 2:13 pm
by feyd

Code: Select all

eval('Test(1, ' + '2, 3' + ');');

Posted: Wed Jul 04, 2007 2:18 pm
by superdezign
Since the '1' would be replaced with an object later on, I'm not too sure that'd work. However, this guy over at TheScripts just pointed me to the apply() method of the Function object, and I've figured it out.

Code: Select all

Test.apply(this, [1].concat(eval('[2, 3]')));