JavaScript Arguments

arguments

The arguments object provides access to the arguments that are passed into a function.

You can refer to a function’s arguments within the function by using the arguments array. This array contains an entry for each argument passed to the function.

The arguments array is available only within a function body. Attempting to access the arguments array outside a function declaration results in an error.

eg.

function Average()
{
   var sum = 0
   for(var i=0; i < arguments.length; i++)
   {
      sum = sum + arguments[i]
   }
   return (sum/arguments.length);
}

You can use the arguments array if you call a function with more arguments than it is formally declared to accept. This technique is useful for functions that can be passed a variable number of arguments.

You can use arguments.length to determine the number of arguments passed to the function, and then process each argument by using the arguments array. (To determine the number of arguments declared when a function was defined, use the Function.length property.)

Example.

function listItems() 
{
   var items = arguments.length
   document.write("<UL>n")
   for (i = 0;i < items;i++)
   {
      document.write("<LI>" + arguments[i] + "n")
   }
   document.write("</UL>n")
}

The function may be called as follows:

listItems("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Happyday");

 

The arguments object has some very handy members.

arguments.length

The number of arguments passed into the function.

arguments.callee

This property is a reference to the function that you are in. Which is very handy if you want to get a reference to an anonymous function from inside itself. One good reason for doing this would be in a recursive anonymous function.

function makeFactorialFunc()
{
   return function(x)
   {
      if (x <= 1)
       {
         return 1;
       }
       return x * arguments.callee(x - 1);
   };
}

var result = makeFactorialFunc()(5); // returns 120 (5 * 4 * 3 * 2 * 1)

arguments.caller

This property is a reference to the function that called the function you are in. This allows you to get access to the entire current callstack which is very handy for debugging.

function myFunc()
{
  if (arguments.caller == null)
  {
    return ("The function was called from the top!");
  }
  else
  {
    return ("This function's caller was " + arguments.caller);
  }
}

 

There are two other things you can do with the arguments object if you combine it with the fn.apply function.

Pass arguments from function to function.

function Called(a,b)
{
	return (a + " certainly " +  b);
}

function Caller(a,b)
{
	return(Called.apply(this,arguments));
}

Fake arguments by creating an array

function Called(a,b)
{
	return (a + " certainly " +  b);
}

function FakeCaller(a,b)
{
	var l_arguments = new Array();

	l_arguments[l_arguments.length] = a;
	l_arguments[l_arguments.length] = nb;

	return(Called.apply(this,l_arguments));
}

See this test-case for an example of both of these cases.

About James McParlane

CTO Massive Interactive. Ex Computer Whiz Kid - Now Grumpy Old Guru.
This entry was posted in JavaScript, MetaWrap Server, Web2.0. Bookmark the permalink.

7 Responses to JavaScript Arguments

  1. leisa says:

    i’ve been having some javascript arguments lately, but they looked nothing like this 😉

  2. Did you win or throw an exception?

  3. leisa says:

    neither actually. Turns out it was a fake argument 😉

    (oh no! am I accidentally learning!)

  4. Rob says:

    Hi James,

    I just want to comment on your ‘arguments’ blog entry. You state many times that ‘arguments’ is an array, which is true in Opera but not in other browers. It is described in the ECMAScript spec as a list, an object that has an array-like ‘length’ property and numeric property names.

    In most browsers, arguments has a length property but none of the other good stuff that arrays have.

    However, you can give arguments the properties of an array. To augment the arguments object of a single function:

    function x()
    {
    arguments.join = Array.prototype.join;
    alert(arguments.join(","));
    }

    Or to augment the arguments object in general:

    Object.prototype.join = Array.prototype.join;

    Cheers, Rob.

  5. peterh says:

    arguments.caller is deprecated (see http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Functions:arguments:caller)

    Your example for it will not run on Mozilla.

    You can work around this by substituting arguments.callee.caller (looks weird, but works), like this:
    function func1(){
    if (arguments.callee.caller == null){
    alert(‘func1 called from the top’)
    } else {
    alert(‘func1 called from ‘ +arguments.callee.caller.name)
    }
    }

  6. Very Educative…

    But not complete for what I was looking for. Easily get arguments in one string.

    Finally I got 3 different solutions with the less code possible, one involving your prototype solution.

    You can see the 3 solutions working in: http://txopi.com/mylab/js/args.concat.html

  7. Ally says:

    Thank God! Someone with bnrais speaks!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s