Home  >  Article  >  Web Front-end  >  javascript arguments usage example

javascript arguments usage example

高洛峰
高洛峰Original
2017-01-04 17:20:061213browse

<script Language="JavaScript">
//第一个参数值. 
function test(a,b,c,d){ 
  alert(arguments[0]); 
} 
//arguments[0]实际上就是a,同理,arguments[1]就是b,依次c,d
</script>
 <script Language="JavaScript">
{
    function function_Name(exp1,exp2,exp3,exp4)
    {
        var umber="";
        umber=arguments.length;
        alert(umber);
    }
            function_Name(&#39;a&#39;,&#39;b&#39;,&#39;c&#39;,&#39;d&#39;);         都可以调用
        //    function_Name("a","b","c","d");     都可以调用
}
</script>

1. In JavaScript, the arguments object is a special object. It is actually a built-in property of the current function. arguments is very similar to Array, but is not actually an Array instance. This can be confirmed by the following code (of course, in fact, in the function funcArg, it is not necessary to write funcArg.arguments when calling arguments, just write arguments directly).

 Array.prototype.testArg = "test";
 function funcArg() {
     alert(funcArg.arguments.testArg);  
     alert(funcArg.arguments[0]);
 }
 alert(new Array().testArg); // result: "test"
 funcArg(10);                // result: "undefined"  "10"

2. The length of the arguments object is determined by the number of actual parameters rather than the number of formal parameters. Formal parameters are variables that are re-opened in memory space within the function, but they do not overlap with the memory space of the arguments object. When both arguments and values ​​exist, the two values ​​are synchronized, but when one of them has no value, the value will not be synchronized for this valueless case. The following code can be verified.

 function f(a, b, c){
     alert(arguments.length);   // result: "2"
     a = 100;
     alert(arguments[0]);       // result: "100"
     arguments[0] = "qqyumidi";
     alert(a);                  // result: "qqyumidi"
     alert(c);                  // result: "undefined"
     c = 2012;
     alert(arguments[2]);       // result: "undefined"
 }
 f(1, 2);

3. From the declaration and calling characteristics of functions in JavaScript, it can be seen that functions in JavaScript cannot be overloaded.

According to the basis for overloading in other languages: "The function return value is different or the number of formal parameters is different", we can draw the above conclusion:

First: The declaration of the Javascript function is not The return value type statement;

Second: Strictly speaking, the number of formal parameters in JavaScript is only to facilitate variable operations in the function. In fact, the actual parameters have been stored in the arguments object.

In addition, let’s deeply understand why functions in JavaScript cannot be overloaded from the JavaScript function itself: In JavaScript, functions are actually objects, and the function name is a reference to the function, or the function name itself is a variable. For the function declaration and function expression shown below, the meaning is the same as above (without considering the difference between function declaration and function expression), which is very helpful for us to understand the feature that functions in JavaScript cannot be overloaded. .

 function f(a){
     return a + 10;
 }
 function f(a){
     return a - 10;
 }
 // 在不考虑函数声明与函数表达式区别的前提下,其等价于如下
 var f = function(a){
     return a + 10;
 }
 var f = function(a){
     return a - 10;
 }

4. There is a very useful attribute in the arguments object: callee. arguments.callee returns the current function reference in which this arguments object resides. It is recommended to use arguments.callee instead of the function name itself when using recursive function calls.

is as follows:

 function count(a){
     if(a==1){
         return 1;
     } 
     return a + arguments.callee(--a);
 }
 var mm = count(10);
 alert(mm);

For more javascript arguments usage examples and related articles, please pay attention to the PHP Chinese website!


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn