Apply your JS skills to key Mozilla projects as an MDN Fellow! http://mzl.la/MDNFellowship

mozilla
您的搜索结果

    arguments

    这篇翻译不完整。请帮忙从英语翻译这篇文章

    简述

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

    语法

    arguments

    描述

    arguments 对象可以在所有方法体内 被获取到的本地变量; 但是不能把arguments当作Function的一个属性.

    在方法体内,你可以通过 arguments 对象获取到所有的参数。这个对象为每个传给function的参数建立一个索引,索引号是从0开始。 例如,如果一个function有三个参数,你可能通过以下方式获取参数:

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

    参数也可以被重新赋值:

    arguments[1] = 'new value';

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

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

    You should not slice on arguments because it prevents optimizations in JavaScript engines (V8 for example).

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

    var args = Array.slice(arguments);

    The arguments object is available only within a function body. Attempting to access the arguments object outside a function declaration results in an error.

    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
    Reference to the currently executing function.
    arguments.caller
    Reference to the function that invoked the currently executing function.
    arguments.length
    Reference to the number of arguments passed to the function.

    例子

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

    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
      Draft  

      兼容性

      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, brandonzhu, teoli
    最后编辑者: brandonzhu,