Tho*_*ing 339

使用arguments.您可以像数组一样访问它.使用arguments.length的参数的数目.

  • 这仅适用于传统的 JavaScript `function`,不适用于 ES2015+ 粗箭头 `=>` 函数。对于那些,您需要在函数定义中使用 `...args`,如下所示:`(...args) => console.log(args)`。 (39认同)

Luk*_*uke 134

所述参数类似阵列的对象(不是实际的阵列).功能示例......

function testArguments () // <-- notice no arguments specified
{
    console.log(arguments); // outputs the arguments to the console
    var htmlOutput = "";
    for (var i=0; i < arguments.length; i++) {
        htmlOutput += '<li>' + arguments[i] + '</li>';
    }
    document.write('<ul>' + htmlOutput + '</ul>');
}
Run Code Online (Sandbox Code Playgroud)

试试看...

testArguments("This", "is", "a", "test");  // outputs ["This","is","a","test"]
testArguments(1,2,3,4,5,6,7,8,9);          // outputs [1,2,3,4,5,6,7,8,9]
Run Code Online (Sandbox Code Playgroud)

完整详情:https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Functions_and_function_scope/arguments

  • 为什么不在这里发布结果呢?:) (3认同)
  • 这比接受的答案要好得多,因为它包含一个工作代码片段并显示输出.接受的答案太稀疏了. (2认同)

Gun*_*ion 28

ES6允许一个构造,其中函数参数用"......"符号指定,例如

function testArgs (...args) {
 // Where you can test picking the first element
 console.log(args[0]); 
}
Run Code Online (Sandbox Code Playgroud)

  • 这也称为“其余参数”,请参阅 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/rest_parameters。 (3认同)
  • 这似乎是使用箭头功能的唯一方法.`a =()=> {console.log(arguments);}; a('foo');`给` - 未捕获的ReferenceError:未定义参数`但是`a =(... args)=> {console.log(args);}; a('foo');`给`["foo"]` (2认同)
  • @DavidBaucum 这是正确的。因为箭头函数不会创建新的作用域,并且“参数”是从作用域中收集的。但最坏的情况不是引用错误。“参数”是从外部范围收集的。然后你的应用程序中就不会出现异常,甚至可能会出现奇怪的错误。 (2认同)

iCo*_*nor 21

arguments对象是存储函数参数的位置.

arguments对象的行为和看起来像一个数组,它基本上是,它只是没有数组所做的方法,例如:

Array.forEach(callback[, thisArg]);

Array.map(callback[, thisArg])

Array.filter(callback[, thisArg]);

Array.slice(begin[, end])

Array.indexOf(searchElement[, fromIndex])

我认为将arguments对象转换为真实数组的最佳方法是这样的:

argumentsArray = [].slice.apply(arguments);
Run Code Online (Sandbox Code Playgroud)

这将使它成为一个阵列;

可重复使用:

function ArgumentsToArray(args) {
    return [].slice.apply(args);
}

(function() {
   args = ArgumentsToArray(arguments);

   args.forEach(function(value) {
      console.log('value ===', value);
   });

})('name', 1, {}, 'two', 3)
Run Code Online (Sandbox Code Playgroud)

结果:

> value === name
> value === 1
> value === Object {}
> value === two
>value === 3


Ima*_*adi 10

如果您愿意,也可以将其转换为数组.如果Array泛型可用:

var args = Array.slice(arguments)
Run Code Online (Sandbox Code Playgroud)

除此以外:

var args = Array.prototype.slice.call(arguments);
Run Code Online (Sandbox Code Playgroud)

来自Mozilla MDN:

您不应该对参数进行切片,因为它会阻止JavaScript引擎中的优化(例如V8).

  • 感谢更新.使用JSON.stringify和JSON.parse作为替代:`function foo(){foo.bar = JSON.stringify(arguments); foo.baz = JSON.parse(foo.bar); 如果需要保存而不是字符串化,请使用[内部结构化克隆算法](http://www.w3.org/TR/html5/infrastructure.html#internal-structured-cloning-algorithm).如果传递了DOM节点,请使用XMLSerializer,如[不相关的问题](http://stackoverflow.com/questions/17273110/1113772).`with(new XMLSerializer()){serializeToString(document.documentElement)}` (2认同)

Las*_*ert 7

正如许多其他指出的那样,arguments包含传递给函数的所有参数.

如果要使用相同的args调用另一个函数,请使用 apply

例:

var is_debug = true;
var debug = function() {
  if (is_debug) {
    console.log.apply(console, arguments);
  }
}

debug("message", "another argument")
Run Code Online (Sandbox Code Playgroud)