JavaScript Tutorial/Function/arguments

Материал из Web эксперт
Перейти к: навигация, поиск

Accessing Function Arguments with the arguments Array

It is possible to access the arguments as an array.



<html>
<SCRIPT LANGUAGE="JavaScript1.2">
<!--
    function displayArguments()
    {
      document.write("The following arguments were passed:<BR>");
      for(i=0; i<arguments.length; i++)
      {
        document.write(i," = ",arguments[i],"<BR>");
      }
    }
    displayArguments(3,"AAA",-7,"BBB");
-->
</SCRIPT>
</html>


arguments

Syntax



arguments
    arguments[index]


The Arguments object is an array that contains all the arguments passed into the function.

The Arguments object is automatically created and initialized when a function is invoked.

The Arguments object goes out of scope when the code function finishes executing.

To access arguments passed into a function, simply use array brackets to specify an index.

Properties Associated with the Arguments Object is listed in the following table.

Property Description callee Contains the function that is currently executing caller Contains the Arguments object of the calling function length The length of the arguments array

arguments.callee

The callee property contains the function that is currently executing.

This is useful if the function has no name.



<html>
    <form>
    <input type="button" value="A" OnClick=myFunction(this,"A")>
    <input type="button" value="B" OnClick=myFunction(this,"B")>
    <input type="button" value="C" OnClick=myFunction(this,"C")>
    <input type="button" value="D" OnClick=myFunction(this,"D")>
    <input type="button" value="E" OnClick=myFunction(this,"E")>
    </form>
    <script language="JavaScript">
    <!--
    function myFunction()
    {
      var aString = arguments[0].value;
      aString += ""s is ";
      aString += arguments[1];
      alert(aString);
      alert(arguments.callee.toString());
    }
    -->
    </script>
    </html>


arguments.caller

The caller property contains the Arguments object of the calling function.

If the given function was not executed from another function, arguments.caller is null.



<html>
    <form>
    <input type="button" value="A" OnClick=myFunction(this,"A")>
    <input type="button" value="B" OnClick=myFunction(this,"B")>
    <input type="button" value="C" OnClick=myFunction(this,"C")>
    <input type="button" value="D" OnClick=myFunction(this,"D")>
    <input type="button" value="E" OnClick=myFunction(this,"E")>
    </form>
    <script language="JavaScript">
    <!--
    function displayArgLength()
    {
      var argLengthStr = "The calling function contained ";
      argLengthStr += arguments.caller.length;
      argLengthStr += " arguments.";
      alert(argLengthStr);
    }
    function myFunction()
    {
      var aString = arguments[0].value;
      aString += ""s is ";
      aString += arguments[1];
      alert(aString);
      displayArgLength();
    }
    -->
    </script>
    </html>


arguments.length

The length property contains the number of arguments that were passed into the function.

This number matches the number of elements in the arguments array associated with the Argument object.



<html>
    <form>
    <input type="button" value="A" OnClick=myFunction(this,"A","B","C")>
    <input type="button" value="B" OnClick=myFunction(this,"D","E")>
    <input type="button" value="C" OnClick=myFunction(this,"F")>
    <input type="button" value="D" OnClick=myFunction(this,"G","H")>
    <input type="button" value="E" OnClick=myFunction(this,"I")>
    </form>
    <script language="JavaScript">
    <!--
    function myFunction()
    {
      var aString = arguments[0].value;
      aString += ""s are: ";
      for(var i=1; i<arguments.length; i++)
      {
        aString += arguments[i];
        aString += ", ";
      }
      alert(aString);
    }
    -->
    </script>
    </html>


Get function parameter length

<html>
<head>
<title>Try Catch Example</title>
<script type="text/javascript">
function addTwoNumbers(a, b) {
    alert(arguments.length);
}
var result = addTwoNumbers(90);
</script>
</head>
<body>
</body>
</html>


Use arguments.callee.toString() to get the source of the function

<html>
    <form>
    <input type="button" value="A" OnClick=displayFood(this,"A")>
    <input type="button" value="B" OnClick=displayFood(this,"B")>
    <input type="button" value="C" OnClick=displayFood(this,"C")>
    <input type="button" value="D" OnClick=displayFood(this,"D")>
    <input type="button" value="E" OnClick=displayFood(this,"E")>
    </form>
    <script language="JavaScript">
    <!--
    function displayFood()
    {
      var aString = arguments[0].value;
      aString += ""s is ";
      aString += arguments[1];
      alert(aString);
      alert(arguments.callee.toString());
    }
    -->
    </script>
    </html>