arguments小笔记

为什么说arguments是伪(类)数组?
答:因为arguments它不是数组,却用起来像数组,有length属性和[ ]访问成员。但是不具有数据的方法,如join()concat()等。。。

怎样将arguments转换成数组

Array.prototype.slice.apply(arguments)
arguments小笔记_第1张图片
image.png

转换前


arguments小笔记_第2张图片
image.png

转换后


arguments小笔记_第3张图片
image.png

1、数组长度

window.onload = function(){

        function abc(){
            console.log(arguments.length)
        }
        abc(1,2,3)

  }// 3

2、改变参数值

window.onload = function(){

        function abc(x,y,z){
              arguments[2] = "hello";
              for(var i=0;i<=arguments.length;i++){
                    console.log(" "+arguments[i]);
              }

          }
        abc(1,2,3)

  }// 1,2,hello

3、递归(callee()调用自身)

求1到n的自然数之和

function add(x){
            if(x == 1) return 1;
            else return x + arguments.callee(x-1);
         }
         console.log(add(5))//15

对于没有命名的函数

var result = function(x){
              if(x == 1) return 1;
              return x+arguments.callee(x-1);
          }
         console.log(result(5))//15

你可能感兴趣的:(arguments小笔记)