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

    一、基础

    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

  • 相关阅读:
    运维工程师的四个阶段
    必须知道的Linux内核常识详解
    Centos7/RHEL7 开启kdump
    sort实现ip排序
    如何判断是否开启超线程
    Django---进阶12
    Django---进阶11
    前端---进阶8
    前端---进阶7
    前端---进阶6
  • 原文地址:https://www.cnblogs.com/king1302217/p/13267311.html
Copyright © 2011-2022 走看看