Get the most out of the JavaScript arguments object

So if you're a CF'er who has doubled down on AJAX then you really should take a minute to get to know the JavaScript arguments object.

In a nutshell, any function object in JavaScript 1.4 or beyond contains a super awesome arguments object. Why is this important? Well it matters if your function needs to make decisions based on the existence of expected arguments, set the value of an argument, count the number of passed arguments and a whole lot of other scenarios.

So for starters, please keep in mind that although the arguments object might look like an array, it is not an array. If you need it to be a true array then it can be converted into one in short order doing something like this...

view plain print about
1var myArguments = Array.prototype.slice.call(arguments);

Another obvious but important point to remember is that the arguments object can only be referenced inside of the function's body. If you try to manipulate it outside of the body of the function a massive energy displacement will occur like when Desmond failed to press "the button" on LOST. Unlike Desmond you cannot survive a large scale electromagnetic event so do not try to access the arguments object outside of the function's body (you have been warned).

Contrary to what some folks might tell it is also possible (and sometimes very useful) to call a function with a greater number of arguments then your function was originally designed to accept. To analyze exactly what your function received at the time it was called you can use the object's length property.

view plain print about
1var myArgsLen = arguments.length;
2if (myArgsLen < 2){
3 alert('Hey coder guy would you mind passing me some stuff before you bother calling me - mkay.');
4}

Finally, we've got the arguments.callee property. Yes, like Whitney Houston I've went and saved the best for last. Why? Well because you can use arguments.callee in an anonymous recursive function like so...

view plain print about
1function makeFactorialFunc() {
2 alert('making a factorial function!');
3 return function(x) {
4 if (x <= 1)
5 return 1;
6 return x * arguments.callee(x - 1);
7 };
8}
9
10var result = makeFactorialFunc()(5); // returns 120 (5 * 4 * 3 * 2 * 1)

Please note that I plagiarized the last snippet of code from mozilla.org because I'm lazy.

Anyway, there you have it - a few good reasons to make friends with the JavaScript arguments object.

Comments
batterie's Gravatar When they finally reach the company, they will then rush into the crowded elevators.
# Posted By batterie | 7/20/10 8:46 AM
labatterie's Gravatar such as Shanghai spend less than an hour on their way to work.
# Posted By labatterie | 7/20/10 8:47 AM
r4 ds's Gravatar It wastes tremendous effort to take several hours on their way from home to company.
# Posted By r4 ds | 7/20/10 8:48 AM
handychina's Gravatar It is very interesting Sciphones i68,ciphone c6,Ciphone,cect handy,Sciphones ,cecthandys,hiphone,HIPHONE 4,pinphone 3gs?Grosshandel in http:/www.efox-shop.com/
# Posted By handychina | 7/23/10 11:29 PM
handychina's Gravatar It is very interesting Sciphones i68,ciphone c6,Ciphone,cect handy,Sciphones ,cecthandys,hiphone,HIPHONE 4,pinphone 3gs?Grosshandel in http:/www.efox-shop.com/.
# Posted By handychina | 7/23/10 11:49 PM
dc comic hats,purple monster energy hat's Gravatar what you provide is very good,i like it so much ,thank you ,the article you write is perfect!
# Posted By dc comic hats,purple monster energy hat | 7/27/10 6:30 AM
ciphone's Gravatar Hello!everyone!iphone is so expensive.so I like <a href=" www.efox-shop.com/index.php/cPath/5_35
" rel="nofollow"> Ciphone </a>. d^_^bIt is very interesting.I want to introduce <a href="http://www.efox-shop.com/"; rel="nofollow"> efox-shop</a> to you.
# Posted By ciphone | 7/30/10 11:24 PM
NFL Jerseys's Gravatar Good article, Each and every point is good enough.Thanks for sharing with us your wisdom.
http://www.nfljerseyspaypal.com cheap nfl jerseys
http://www.nfljerseysgo.com NFL Shop
http://www.nfljerseysgo.com NFL Jerseys
http://www.eightshop.com polo shirts
# Posted By NFL Jerseys | 8/23/10 11:56 PM
gucci sunglasses's Gravatar I was actually looking for this resource a few weeks back. Thanks for sharing with us your wisdom.This will absolutely going to help me in my projects .
http://www.onesunglasses.com oakley sunglasses
http://www.onesunglasses.com ray ban sunglasses
http://www.onesunglasses.com gucci sunglasses
# Posted By gucci sunglasses | 8/23/10 11:57 PM
gucci sunglasses's Gravatar I was actually looking for this resource a few weeks back. Thanks for sharing with us your wisdom.This will absolutely going to help me in my projects .
http://www.onesunglasses.com oakley sunglasses
http://www.onesunglasses.com ray ban sunglasses
http://www.onesunglasses.com gucci sunglasses
# Posted By gucci sunglasses | 8/23/10 11:58 PM
Cowboys shop's Gravatar http://www.nflsaintsshop.com/saints-jerseys/garrett-hartley-jerseys Garrett Hartley Jerseys
http://www.nflsaintsshop.com saints shop
http://www.nflsaintsshop.com/saints-jerseys/drew-brees-jerseys Drew Brees Jerseys
# Posted By Cowboys shop | 9/4/10 12:24 AM
BlogCFC was created by Raymond Camden. This blog is running version 5.9.5.007. Contact Blog Owner