eval() can't make function arguments?

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

eval() can't make function arguments?

Post 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?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Code: Select all

eval('Test(1, ' + '2, 3' + ');');
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post 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]')));
Post Reply