JavaScript 参数数组 [英] JavaScript arguments array

查看:30
本文介绍了JavaScript 参数数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 JavaScript 的新手,遇到了这个片段:

I am new to JavaScript and came across this snippet:

function addSuffix()
{
    var sString= " ";
    for (var k=1, k<arguments.length; k++)
    {
         sString +=arguments[k] + arguments[0] + " " ;
    } 
    return sString;
 }

 console.info(addSuffix('s','bow','dog','kite'));

谁能解释一下.(我已经阅读了 Java 脚本并了解循环等,但这个例子主要是因为参数数组让我感到困惑)

Can somebody explain. (I have read about java script and know abouts loops etc but this example confused me mainly because of the arguments array)

推荐答案

arguments 数组是一种将无限"值传递给函数的方法,而无需指定每个值.

The arguments array is a way to pass "unlimited" values to a function, without having to specify every single one of them.

可以将其视为使函数接收未指定数量的值的一种方式.在您的示例中,这相当于说:

Think of it as a way to make a function recieve a non-specified number of values. In your example, it would be the same as saying:

function addSuffix(argument1, argument2, argument3)
{
    var sString = argument2+argument1+" "+argument3+argument1+" ";
    return sString;
 }

因为它始于 1(传递的第二个参数),然后再次添加第一个(arguments[0]),然后添加一个空格(").然后它重复这个过程.

Because it starts from 1 (the second argument passed), then adds the first one again (arguments[0]) then a white space (" "). Then it repeats the process.

Mozilla 开发者网络.

这篇关于JavaScript 参数数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆