rsk*_*k82 209 javascript algorithm arguments function marshalling
在PHP中有func_num_args和func_get_argsJavaScript有类似的东西吗?
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>');
}
试试看...
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]
完整详情:https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Functions_and_function_scope/arguments
Gun*_*ion 28
ES6允许一个构造,其中函数参数用"......"符号指定,例如
function testArgs (...args) {
 // Where you can test picking the first element
 console.log(args[0]); 
}
iCo*_*nor 21
该arguments对象是存储函数参数的位置.
arguments对象的行为和看起来像一个数组,它基本上是,它只是没有数组所做的方法,例如:
Array.forEach(callback[, thisArg]);
Array.map(callback[, thisArg])
Array.filter(callback[, thisArg]);
Array.indexOf(searchElement[, fromIndex])
我认为将arguments对象转换为真实数组的最佳方法是这样的:
argumentsArray = [].slice.apply(arguments);
这将使它成为一个阵列;
可重复使用:
function ArgumentsToArray(args) {
    return [].slice.apply(args);
}
(function() {
   args = ArgumentsToArray(arguments);
   args.forEach(function(value) {
      console.log('value ===', value);
   });
})('name', 1, {}, 'two', 3)
结果:
>
value === name
>value === 1
>value === Object {}
>value === two
>value === 3
Ima*_*adi 10
如果您愿意,也可以将其转换为数组.如果Array泛型可用:
var args = Array.slice(arguments)
除此以外:
var args = Array.prototype.slice.call(arguments);
来自Mozilla MDN:
您不应该对参数进行切片,因为它会阻止JavaScript引擎中的优化(例如V8).
正如许多其他指出的那样,arguments包含传递给函数的所有参数.
如果要使用相同的args调用另一个函数,请使用 apply
例:
var is_debug = true;
var debug = function() {
  if (is_debug) {
    console.log.apply(console, arguments);
  }
}
debug("message", "another argument")