zoukankan      html  css  js  c++  java
  • javascript 实参对象 arguments

       在函数体内,标识符arguments是指向实参对象的应用,实参对象是一个数组对象 ,这样就可以通过数字下标访问传入函数的实参值,

          演示arguments 的用法

    View Code
    //查找最大值
       function maxs()
       {
          var _max=Number.NEGATIVE_INFINITY; //负无穷大,溢出时返回该值
          
          //遍历实参,查找并记录最大值
          for(var i=0;i<arguments.length;i++)
          {
             if(arguments[i]>_max)
             {
                _max=arguments[i];
             }
          }
          
          return _max;
       }
       
         var largest=maxs(1,2,4,10,3,0,33,44);
          console.log(largest);
         
         
          console.log("*********************************************************************");
         function f(x)
         {
           console.log(x);
           arguments[0]=null;
           console.log(x);
           console.log("arguments length:"+arguments.length);  //实际参数
           console.log("期望参数个数:"+arguments.callee.length);
           
         }
         
         f(1);
         

     argument[] 对象最适合的应用场景是 在函数包含固定个数的命名个必须参数,以及随后个数不定的可选参数。arguments 并不是真正的数组。

  • 相关阅读:
    Nightwatch的介绍
    reduce的用法及实例
    什么是声明式渲染?
    H5自带的表单验证
    Flex弹性布局
    JS中的forEach,for in,for of和for的遍历优缺点及区别
    将博客搬至CSDN
    9 外观模式(Facade)
    8 代理模式(Proxy)
    7 装饰模式(Decorator)
  • 原文地址:https://www.cnblogs.com/chenxiao/p/2798608.html
Copyright © 2011-2022 走看看