JavaScript参数数组 [英] JavaScript arguments array

查看:104
本文介绍了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 Developer Network。

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

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