Reputation: 25711
The code below works fine in FF3.X and IE7 to 9, but not in FF4.
I have this code in 2 places in my file:
var arguments = "method=getoptions";
arguments += "&dropzone=" + dictKey;
arguments += "&format=disc";
arguments += "&datasetid=" + datasetid;
arguments += "&varnumber=" + varnumber;
arguments += "&varSectionId=" + varSectionId;
arguments += "&catindex=" + catIndex;
arguments += "&defaultid=dv_cat_opts_default_body";
arguments += "&mmocid=dv_cat_opts_mmoc_body";
alert(arguments);
which produces
method=getoptions&dropzone=Row_1&format=disc&datasetid=1&varnumber=206&varSectionId=FUV&catindex=&defaultid=dv_cat_opts_default_body&mmocid=dv_cat_opts_mmoc_body
and that is correct, but the same code elsewhere in the file
var arguments = "method=getoptions";
arguments += "&dropzone=" + dictKey;
arguments += "&format=disc";
arguments += "&datasetid=" + datasetid;
arguments += "&varnumber=" + varnumber;
arguments += "&varSectionId=" + varSectionId;
arguments += "&catindex=" + catIndex;
arguments += "&defaultid=dv_cat_opts_default_body";
arguments += "&mmocid=dv_cat_opts_mmoc_body";
alert(arguments);
outputs this which ends up calling an error in my ajax:
[object Arguments]&dropzone=Row_1&format=cont&datasetid=1&varnumber=1125&varSectionId=FUV&catindex=&defaultid=dv_cont_opts_default_body&mmocid=dv_cont_opts_mmoc_body
So what's the deal with that object Arguments?
Upvotes: 3
Views: 170
Reputation: 2049
arguments
is a reserved variable in JavaScript functions, which contains the arguments (also known as parameters) of the function.
So, for example, function(a){alert(a);}
would be the same as function(){alert(arguments[0]);}
.
To fix this error, simply change the variable's name to something else.
Ad@m
Upvotes: 1
Reputation: 50602
The variable arguments
is a special object available within functions, it contains all the arguments passed to a function.
https://developer.mozilla.org/en/JavaScript/Reference/functions_and_function_scope/arguments
To put it another way, consider the variable name arguments
to be a reserved keyword - just like you would never name a variable if
, don't use the variable name arguments
or you will get unexpected results.
Why is this thing here, you might ask - it lets you write your functions that use a varying number and order of arguments. Within your function, you'd write code to determine which arguments were passed and what they are. It takes quite a bit more code, but you can create some pretty flexible reusable code with this technique. Check out some of the internal functions for Mootools or jQuery and you'll see that these popular frameworks make heavy use of the arguments
object.
Consider:
function alertError() {
var exception = false;
var message = false;
for( var i = 0; i < arguments.length; i++ ) {
if (typeof arguments[i] == 'object')
exception = arguments[i];
if (typeof arguments[i] == 'string')
message = arguments[i];
}
if (message == false)
message = 'No details';
if (exception != false)
message += ', exception: '+exception.message;
alert('There has been an error: '+message);
}
alertError("No exceptions here!");
try {
var t = t.doesnotexist;
} catch (e) {
alertError(e, 'Testing');
}
Try it here: http://jsfiddle.net/zwGMJ/
Upvotes: 4