[Javascript Function] Arguments, call(), apply(), caller(), callee()

(1) arguments : length, []

  是一个伪数组,只有数组的基本功能
  接受从外面传来的参数

  - arguments.length
  - arguments[0]
    arguments[1].......
   <script type="text/javascript">
      function a(){
      console.log(arguments.length);
      console.log(arguments[2]);
      }
      a(1,2,3,4,123,345);
       //6
       //3
   </script>

(2) arguments 伪数组 转化为 数组

Array.prototype.slice.call(arguments);//转换

  eg: 
  function a(){
      console.log(arguments.sort());//因为是伪数组 不能享受此功能
      }
  a(1,2,3,4,123,345);

Array.prototype.slice.call(arguments);//转换

   <script type="text/javascript">
    function a(){
    var argu=Array.prototype.slice.call(arguments);
    console.log(argu.sort());
    console.log(argu.join());    
    }
    a(12,345,1);//[1, 12, 345]
                //"12,345,1"
   </script>

(3) caller callee

caller 调用者
callee 调用自己

- callee 
  var sum = function(n){ 
    if (n <= 0)  
      return 1; 
    else 
      return n+arguments.callee(n-1); 
      //return n+sum(n-1); 一样的
  }  
  alert(sum(5));

(4) apply call

相同点:

调用不一样的对象去执行 

不同点:

apply(obj,参数以数组形式);
call(obj,参数1,参数2,参数3,….,.,.,.,…);

<script type="text/javascript">
     function add(x,y){
       console.log(x+y);
      }
     function a(x,y){
       add.call(this,x,y);
       //add.apply(this,arguments); 一样
       //this.add(x,y);一样
      }
     a(2,5);
</script>

Summary:

      add.call(this,x,y);
      add.apply(this,arguments);
      this.add(x,y);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值