JavaScript函数中的无限参数

时间:2011-06-18 12:37:19

标签: javascript arrays function

JavaScript函数可以采用无限制的参数吗? 像这样:

testArray(1, 2, 3, 4, 5...);

我在尝试:

var arr = [];
function testArray(A) {
    arr.push(A);
}

但这不起作用(输出只是第一个参数)。或者唯一的方法是:

function testArray(a, b, c, d, e...) {

}

由于

9 个答案:

答案 0 :(得分:54)

你可以引用一个奇怪的“魔法”变量叫做“参数”:

function manyArgs() {
  for (var i = 0; i < arguments.length; ++i)
    alert(arguments[i]);
}

喜欢一个数组,但它不是一个数组。事实上,你真的不应该使用它真是太奇怪了。通常的做法是将其值转换为真正的数组:

function foo() {
  var args = Array.prototype.slice.call(arguments, 0);
  // ...

在那个例子中,“args”将是一个普通的数组,没有任何奇怪的东西。 “参数”存在各种令人讨厌的问题,在ECMAScript 5中,其功能将受到限制。

编辑 - 尽管使用.slice()函数确实很方便,但事实证明将arguments对象从函数中传出会导致优化问题,以至于执行此操作的功能可能根本无法优化。因此,将arguments转换为数组的简单直接方法是

function foo() {
  var args = [];
  for (var i = 0; i < arguments.length; ++i) args[i] = arguments[i];
  // ...
}

More about arguments and optimization.

答案 1 :(得分:18)

从ECMAScript 2015(或ES6)开始,我们还可以访问rest parameters,这为我们提供了一种更清晰的方式来管理参数:

function foo(a, b, ...others) {
    console.log("a and b are ", a, b);

    for (let val of others) {
        console.log(val);
    }
}

foo(1, 2, 3, 4, 5);

在撰写本文时,Chrome 47 +,Firefox 15+和Edge都支持此功能。该功能也可以通过BabelTypeScript转发到ES5。

答案 2 :(得分:5)

使用ECMAScript 6,您可以使用其余的参数语法:

const testArray = (...args) => {
    console.log(args);
};

testArray(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);

答案 3 :(得分:1)

function toArray() {
   return arguments;
}

var myargs = toArray(1, 2, 3, 4, 5, 6);

每个js函数都有arguments关键字

答案 4 :(得分:1)

var arr = [];
function testArray() {
    Array.prototype.push.apply(arr, arguments);
}

答案 5 :(得分:1)

你也可以“施放”它,为你节省丑陋的循环:

var getArguments = function() {
    return arguments;
};

var foo = getArguments(1,2,3,4);

// console.log(foo.slice()); => TypeError: foo.slice is not a function

var foo = Object.values(foo); 

console.log(foo); // => [ 1, 2, 3, 4 ]

foo.push(5);

console.log(foo); // => [ 1, 2, 3, 4, 5 ]

答案 6 :(得分:1)

有一些遗留方法,但是我更喜欢ES6和更高版本,因此,如果我想实现这一点,我将其编写如下:

const func = ...arg => console.log(arg);

简单而尖端的技术。

答案 7 :(得分:0)

Javascript ES5

              [0]                 [1]
          [0]   [1]  [2]     [0]    [1]  [2]
list = [['Bob', 13 ,156], ['Jonny', 24, 180]]

list[0][0] = 'Bob'
list[1][2] = 180

Javascript ES6

for i in list:
    age = i[1]
    if age > 18:
        print("Age is greater than 18", i)

答案 8 :(得分:0)

const outputDiv=document.querySelector('.output');
const output=(...x)=>{
    return outputDiv.innerHTML=x;
}

output(1,2,3,4,['hello',5,6],true,null);
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Javascript Practice</title>
    <link href="https://fonts.googleapis.com/css2?family=Raleway:ital,wght@0,100;0,200;0,300;0,400;0,500;0,600;0,700;0,800;0,900;1,100;1,200;1,300;1,400;1,500;1,600;1,800;1,900&display=swap" rel="stylesheet">


    <style>
        body{font-family: 'Raleway', sans-serif; background-color: #060606;}
        .center{height:100vh; width: 100%; display: grid;align-items:center;justify-content: center;}
        .output{font-size: 15px;color: rgb(59, 59, 255);font-weight: 200;}
    </style>
    
</head>
<body>
    <div class="center">
        <div class='output'></div>
    </div>

    <script src="js.js"></script>
</body>
</html>

相关问题