JavaScript Tutorial/Function/arguments

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

Accessing Function Arguments with the arguments Array

It is possible to access the arguments as an array.



   <source lang="javascript">

<html> <SCRIPT LANGUAGE="JavaScript1.2">

</SCRIPT> </html></source>


arguments

Syntax



   <source lang="javascript">

arguments

   arguments[index]</source>
   
  

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.



   <source lang="javascript">

<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">
   
   </script>
   </html></source>
   
  

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.



   <source lang="javascript">

<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">
   
   </script>
   </html></source>
   
  

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.



   <source lang="javascript">

<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">
   
   </script>
   </html></source>
   
  

Get function parameter length

   <source lang="javascript">

<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></source>


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

   <source lang="javascript">

<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">
   
   </script>
   </html></source>