Javascript function argument


Javascript functions allow parameters (but not the parentheses that contain them), or you can pass parameters to the function to use.

In the following example, the hello () function is passed the name and age parameters, the value of the parameters are John and 18.

<html>
   <head>
      <script type="text/Javascript">
      function hello(name,age) {
          document.write("My name is " + name + ", I'm " + age + " years old!");
      }
      </script>
   </head>
   <body>
      <input type="button" onclick="hello('John',18)" value="Confirm" />
   </body>
</html>

Run the example, click the OK button, the output:

 My name is John, I'm 18 years old!

Javascript function parameter error

Javascript function parameters are not strictly required which parameters are required parameters, which parameters are optional parameters, so the number of parameters passed is allowed to define the function is not equal to the number of parameters.

If an undefined parameter is used in the function, a syntax error (undefined parameter) is indicated and the Javascript code does not run properly.

If the parameter is already defined, but the parameter is passed incorrectly, the associated parameter value is replaced with undefined, and the Javascript code continues to function as shown in the following example:

<html>
   <head>
      <script type="text/Javascript">
      function hello(name,age) {
          document.write("My name is " + name + ", I'm " + age + " years old!");
      }
      </script>
   </head>
   <body>
      <input type="button" onclick="hello('John')" value="Confirm" />
   </body>
</html>

Run the example, click the OK button, the output:

My name is John, undefined this year!

JavaScript arguments object

In the JavaScript function, there is a special arguments object, it is similar to the form of an array to save the current function call parameters. Therefore, developers do not need to define a specific parameter name, you can easily access the function parameters:

<html>
   <head>
   <script type="text/Javascript">
      function hello() {
          document.write("My name is " + arguments[0] + ", I'm " + arguments[1] + " years old!");
      }
      </script>
   </head>
   <body>
      <input type="button" onclick="hello('John',18)" value="Confirm" />
   </body>
</html>

Run the example, click the OK button, the output:

 My name is Xiao Ming, 18 years old this year!

In the arguments object, arguments [0] represents the first argument, arguments [1] represents the second argument, and so on.

Note

Usually in the function definition, in order to facilitate the readability of the code, generally do not use the arguments object. It is convenient to use the arguments object when dealing with an indefinite number of arguments, or when simulating a function overload.


Number of detection parameters

Using arguments object, you can easily detect the number of function parameters. Arguments of the length property, arguments.length is the number of parameters:

<html>
   <head>
      <script type="text/Javascript">
         function hello() {
          document.write(arguments.length);
         }
      </script>
   </head>
   <body>
      <input type="button" onclick="hello('John',18)" value="Confirm" />
   </body>
</html>

Run the example, click the OK button, the output:

2



Copyright 2014-2017 by DeveloperTut.com. All Rights Reserved.
TwitterGoogle+Facebook