MDN may have intermittent access issues April 18 13:00 - April 19 01:00 UTC. See whistlepig.mozilla.org for all notifications.

mozilla
您的搜索结果

    arguments

    翻译正在进行中。

    arguments 是一个类数组对象。代表传给一个function的参数列表。

    语法

    arguments

    描述

    arguments 对象是函数内部的本地变量;arguments 已经不再是函数的属性了。

    你可以在函数内部通过使用 arguments 对象来获取函数的所有参数。这个对象为传递给函数的每个参数建立一个条目,条目的索引号从 0 开始。例如,如果一个函数有三个参数,你可以通过以下方式获取参数:

    arguments[0]
    arguments[1]
    arguments[2]
    

    参数也可以被重新赋值:

    arguments[1] = 'new value';

    arguments 对象并不是一个真正的数组。它类似于数组,但没有数组所特有的属性和方法,除了 length。例如,它没有 pop 方法。不过可以将其转换成数组:

    var args = Array.prototype.slice.call(arguments);

    不应在 arguments 对象上使用 slice 方法,这会阻碍 JavaScript 引擎的优化 (比如 V8 引擎)。作为替代,应通过遍历 arguments 对象的方式来构建一个新的数组。

    If Array generics are available, one can use the following instead:

    var args = Array.slice(arguments);

    arguments 对象仅在函数内部有效,在函数外部调用 arguments 对象会出现一个错误。

    You can use the arguments object if you call a function with more arguments than it is formally declared to accept. This technique is useful for functions that can be passed a variable number of arguments. You can use arguments.length to determine the number of arguments passed to the function, and then process each argument by using the arguments object. (To determine the number of arguments declared when a function was defined, use the Function.length property.)

    属性

    arguments.callee
    指向当前执行的函数
    arguments.caller
    Reference to the function that invoked the currently executing function.
    arguments.length
    指向传递给当前函数的参数数量。

    例子

    例子: 定义一个联结字符串的方法

    This example defines a function that concatenates several strings. The only formal argument for the function is a string that specifies the characters that separate the items to concatenate. The function is defined as follows:

    function myConcat(separator) {
      var args = Array.prototype.slice.call(arguments, 1);
      return args.join(separator);
    }

    You can pass any number of arguments to this function, and it creates a list using each argument as an item in the list.

    // returns "red, orange, blue"
    myConcat(", ", "red", "orange", "blue");
    
    // returns "elephant; giraffe; lion; cheetah"
    myConcat("; ", "elephant", "giraffe", "lion", "cheetah");
    
    // returns "sage. basil. oregano. pepper. parsley"
    myConcat(". ", "sage", "basil", "oregano", "pepper", "parsley");

    例子: 定义一个创建HTML列表的方法

    This example defines a function that creates a string containing HTML for a list. The only formal argument for the function is a string that is "u" if the list is to be unordered (bulleted), or "o" if the list is to be ordered (numbered). The function is defined as follows:

    function list(type) {
      var result = "<" + type + "l>
    • "; var args = Array.prototype.slice.call(arguments, 1); result += args.join("
    • "); result += "
    • "; // end list return result; }

      You can pass any number of arguments to this function, and it adds each argument as an item to a list of the type indicated. For example:

      var listHTML = list("u", "One", "Two", "Three");
      
      /* listHTML is:
      
      "
      • One
      • Two
      • Three
      " */

      特性

      Specification Status Comment
      ECMAScript 1st Edition. Standard Initial definition. Implemented in JavaScript 1.1
      ECMAScript 5.1 (ECMA-262)
      Arguments Object
      Standard  
      ECMAScript 6 (ECMA-262)
      Arguments Exotic Objects
      Release Candidate  

      兼容性

      Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
      Basic support (Yes) (Yes) (Yes) (Yes) (Yes)
      Feature Android Chrome for Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
      Basic support (Yes) (Yes) (Yes) (Yes) (Yes) (Yes)

      了解

    文档标签和贡献者

    此页面的贡献者有: ziyunfei, Fadeoc, brandonzhu, teoli
    最后编辑者: teoli,
    隐藏侧边栏