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

  • 相关阅读:
    线程
    网络编程
    正则表达式
    XML
    java----八种排序算法
    Java 关键字 速查表
    Day_19多线程(下)
    Day18_进程(中)
    Day17_进程(上)
    Day15_IO流(上)
  • 原文地址:https://www.cnblogs.com/king1302217/p/13267311.html
Copyright © 2011-2022 走看看