2

Got a weird case where the javascript arguments object is being heavily relied on. I've got the problem 'solved' but was wondering if someone could explain to me some of the behavior that results from this code:

function whatweget(arg1,arg2,arg3) {
    while(arguments.length<3) { arguments[arguments.length++] = undefined; }
    console.log(arguments);
}
function argstest() {
    arguments[arguments.length++] = 3;
    console.log(arguments);
}
whatweget(1,2);
whatweget(1,2,3);
whatweget(1,2,3,4);
argstest(3);

Outputs in Chrome:

[1, 2, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined]
[1, 2, 3]
[1, 2, 3, 4]
[3, 3]

Inspecting the arguments object at the whatweget console.log shows it having 20 items, the first 3 are bolded while the last 17 are slightly faded. Inspecting arguments in the argstest console.log these 'ghost' parameters don't show up. This is not a problem as subsequent scripts will be passed 'arguments' and use the 'length' parameter which is set properly, but was wondering if anyone had any insight into this behavior?

Oh, and btw, Firefox 8 is totally cool with this behavior. It does everything as expected, well, except that it doesn't show the arguments object in the Watch inspector, though it will show it as an array when you hover your mouse over it. Bad Firefox, bad bad firefox.

4
  • The arguments object isn't an array, even though it sort of acts like one. I think you need to convert it to an array before trying to push another element to it. Commented Nov 30, 2011 at 15:23
  • I recognize this, which is why I set a parameter of the arguments object = undefined at [i] (similar to doing arguments.i = undefined) rather than doing arguments.push(undefined). I also manually update the .length parameter via arguments.length++. Added info: the debugger only goes through the whatweget loop one time when 2 parameters are passed, as expected. So where do these ghost values get added from? Commented Nov 30, 2011 at 15:30
  • @Teddy: You can modify properties of the arguments object, as well as its length property. You can also add new ones. Commented Nov 30, 2011 at 15:31
  • This is a good source of additional info: bonsaiden.github.com/JavaScript-Garden/#function.arguments Commented Nov 30, 2011 at 15:35

1 Answer 1

1

If you are testing in the developer console, I think it is a console display bug.

If you do a for-in over the arguments object in order to enumerate all of its properties, you only get the 3 you would expect.

function whatweget(arg1,arg2,arg3) {
    while(arguments.length<3) { arguments[arguments.length++] = undefined; }
    for( var n in arguments ) console.log(n, arguments[n]);
}

0 1
1 2
2 undefined

Or if I return the arguments object, and test for properties using in, it shows the properties are not there.

function whatweget(arg1,arg2,arg3) {
    while(arguments.length<3) { arguments[arguments.length++] = undefined; }
    return arguments;
}

var args = whatweget(1,2);

console.log( '2' in args );  is true
console.log( '5' in args );  is false
Sign up to request clarification or add additional context in comments.

1 Comment

My own tests confirm yours. Seems if you treat the anomalous arguments object normally, the ghost undefineds are not present. Looks to be a Chrome bug. Thank you for investigating with me!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.