导航

javascript arguments对象

Posted on 2020-07-08 15:44  寒宵飞飞  阅读(100)  评论(0编辑  收藏  举报

一、基础

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/arguments

 

二、总结

arguments is an Array-like object accessible inside functions that contains the values of the arguments passed to that function.

1)argument  in arguments can easily be get、set or reassigned  

       arguments[0] // first argument
       arguments[1] = 'new value';

2)The arguments object is not an Array. It is similar, but lacks all Array properties except length

3) it can be converted to a real Array:  var args = Array.prototype.slice.call(arguments);

4)The typeof operator returns 'object' when used with arguments,The type of individual arguments can be determined by indexing arguments:

 

三、示例

function func1(a, b, c) {
    console.log(arguments);
    console.log(Array.prototype.slice.call(arguments,0));
    console.log({length:2,0: 'b',1:'d'});
    console.log(Array.prototype.slice.call({length:2,0: 'b',1:'d'}));
    console.log({0: 'b',1:'d'});
    console.log(Array.prototype.slice.call({0: 'b',1:'d'}));
}

func1(1, 2, 3);

输出 

> Object { 0: 1, 1: 2, 2: 3 }                   //arguments

> Array [1, 2, 3]                                   //arguments can convert to array

> Object { 0: "b", 1: "d", length: 2 }      //Array-like object

> Array ["b", "d"]                                //Array-like object can convert to array

> Object { 0: "b", 1: "d" }                  //normal object

> Array []                                         //normal  object cannot convert to array