Home > php教程 > PHP开发 > body text

Arguments object

高洛峰
Release: 2016-12-14 09:56:44
Original
1131 people have browsed it

The calling object defines a special property, named arguments, which actually refers to a special object Arguments object. Because the Arguments property is a property of the calling object, its state is the same as local variables and formal parameters. arguments.length can get the actual parameter quantity passed to the parameter
Not much nonsense, the example explains everything

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">  
<HTML>  
 <HEAD>  
 </HEAD>  
 <BODY>  
 <SCRIPT LANGUAGE="JavaScript">  
 <!--  
    function f(x,y,z)  
    {  
        if (f.length !=arguments.length)  
        {  
            //可以通过读取函数的length和arguments的length属性值来检查形式参数的数量是否相等  
            //因为前者恰好是形参的数量,而后者是实参数量  
            //如果不相等抛出异常  
            throw new Error(&#39;function f called with&#39; +arguments.length+&#39;arguments,but it expects&#39;+f.length+&#39;arguments&#39;);  
        }  
        else   
        {  
            document.write("f("+[x,y,z]+&#39;)&#39;+&#39;<br/>&#39;);  
        }  
    }  
  
    try  
    {  
        f(1,2,3);  
        f(2,4);//抛出异常,后面的将不再执行  
        f(4);  
        f("a","b","c","d","e","f");  
    }  
    catch (ex)  
    {  
        document.write(ex.message);  
    }  
 //-->  
 </SCRIPT>  
    
 </BODY>  
</HTML>
Copy after login

Running results:

Java code

f(1,2,3)  
function f called with2arguments,but it expects3arguments
Copy after login

arguments behave a bit like an array, but it is not actually an array, it does not It has some methods of JavaScript core arrays such as join, sort, slice, etc.

An example of using arguments object to receive any parameters

Java code

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">  
<HTML>  
 <HEAD>  
 </HEAD>  
  
 <BODY>  
  <SCRIPT LANGUAGE="JavaScript">  
  <!--  
    function f()  
    {// 利用arguments来读取任意个数的参数  
        document.write("f(" +Array.apply(null,arguments) + ")" +"<br/>");//apply()应用某一对象的一个方法,用另一个对象替换当前对象。  
    }  
    f(1,2,3);  
    f("a","b");  
    f(true);  
  //-->  
  </SCRIPT>  
 </BODY>  
</HTML>
Copy after login

An example of using arguments object to simulate function overloading

Java code

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">  
<HTML>  
 <HEAD>  
  <TITLE> New Document </TITLE>  
 </HEAD>  
  
 <BODY>  
  <SCRIPT LANGUAGE="JavaScript">  
  <!--  
    function Point()  
    {  
        if(arguments.length ==0)//如果没有参数  
        {  
            //默认的xy属性都设置0  
            this.x=0;  
            this.y=0;  
            this.toString=function()  
                {  
                    return this.x+" "+ this.y ;  
                }  
        }  
  
        else if (arguments.length <2)//如果实参小于2  
        {  
            var p=arguments[0];  
            if (p instanceof Point )//判断实参类型如果是Point,那么执行属性复制  
            {  
                this.x =p.x;  
                this.y =p.y  
                this.toString=function()  
                {  
                    return this.x+" "+ this.y ;  
                }  
            }  
            else if ( typeof p=="number" || p instanceof Number)//如果是数值,那么这个值作为当前Point的x属性值,y属性默认0  
            {  
                this.x=(Number)(p);  
                this.y=0;  
                this.toString=function()  
                {  
                    return this.x+" "+ this.y ;  
                }  
            }  
            else  
            {//如果这个参数既不是Point又不是Number,抛出类型异常  
                throw new TypeError("参数类型错误!")  
            }  
        }  
        else if (arguments.length==2)  
        {  
            var x= arguments[0];  
            var y= arguments[1];  
            //否则当参数数量为两个并且为number类型的时候,把她们分别作为Point的xy  
            if ((typeof x==&#39;number&#39; || x instanceof &#39;Number&#39;) && typeof y == &#39;number&#39; ||y instanceof &#39;Number&#39;)  
            {  
                this.x = x;  
                this.y = y;  
                this.toString=function()  
                {  
                    return this.x+" "+ this.y ;  
                }  
            }  
            else  
                throw new TypeError(&#39;参数类型错误!&#39;);  
        }  
        else  
        {  
            throw new TypeError("参数类型错误!");  
        }  
  
    }  
    function dwn(s)  
    {  
        document.write(s+"<br/>");  
    }  
    dwn(new Point());//00  
    dwn(new Point(new Point()));//00  
    dwn(new Point(3));//30  
    dwn (new Point(4,5));//45  
  //-->  
  </SCRIPT>  
 </BODY>  
</HTML>
Copy after login

It should be noted that in functions that use named parameters, the parameters in arguments are always aliases of the corresponding named parameters. Regardless of whether the parameter is a value type or a reference type, changing the participation in arguments will definitely affect the corresponding Named parameters, and vice versa, such as:

Java code

function f(x)  
{  
   alert(x);//参数初始值  
   arguments[0]++;//改变参数的值  
   alert(x);//x的值发生了改变  
}
Copy after login

The Arguments object also provides a useful attribute called callee, which is used to apply the currently executing function. It provides an anonymous recursive calling capability. This is very useful for closures, such as:
Calculating the factorial of 10 using closures

Java code

function(x){  
   return x>1?x*arguments.callee(x-1):1  
}(10);
Copy after login


Related labels:
source:php.cn
Statement of this Website
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
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!