Javascript递归参数

Javascript递归参数,javascript,recursion,Javascript,Recursion,我对如何将参数传递到新的递归级别存在问题。以下是我所做工作的简化版本: var newFunction = function(obj) { var result = ""; var args = []; Array.prototype.push.apply(args, arguments); var thisArg = args.shift() //do stuff to add onto result with thisArg. This block works, so I

我对如何将参数传递到新的递归级别存在问题。以下是我所做工作的简化版本:

var newFunction = function(obj) {
  var result = "";
  var args = [];
  Array.prototype.push.apply(args, arguments);
  var thisArg = args.shift()
  //do stuff to add onto result with thisArg. This block works, so I'm skipping.
  if (args.length !== 0) {
    result += newFunction(args);
  };
  return result;
};
我遇到的问题与“args”如何被传递到newFunction以循环返回有关。进行递归回调时,args作为单个数组参数传递到新函数范围:

  • 原始参数=(“字符串”,true,9,“字符串2”)
  • 递归中的新参数=([true,9,字符串2])
它需要:

  • 原始参数=(“字符串”,true,9,“字符串2”)

  • 递归中的新参数=(true,9,“字符串2”)

我很确定这与我如何使用有关。应用args变量。我只是不知道如何避开这个问题,因为你不能.shift()这个'arguments'对象。我的做法是将args设置为数组;因此,当它传入时,它是作为单个数组传入的。这就是我的代码的问题

你知道我做错了什么吗?提前谢谢

您可以使用
.apply()

.apply()
函数类似于
.call
,但它扩展了数组元素,以便(有效地)将数组逐元素复制到新调用函数中的
arguments
对象中。

您可以使用
.apply()

.apply()
函数类似于
.call
,但它扩展了数组元素,以便(有效地)将数组逐元素复制到新调用函数中的
arguments
对象中。

您可以使用
.apply()

.apply()
函数类似于
.call
,但它扩展了数组元素,以便(有效地)将数组逐元素复制到新调用函数中的
arguments
对象中。

您可以使用
.apply()

.apply()
函数类似于
.call
,但它将数组元素向外展开,以便(有效地)将数组逐元素复制到新调用函数中的
arguments
对象中。

在ECMAScript 5中,使用:

var newFunction=function(thisArg){
var结果=”,
args=[].slice.call(参数,1);
// ...
if(args.length)result+=newFunction.apply(void 0,args);
返回结果;
};
在ECMAScript 6中,使用和

var newFunction=function(thisArg,…args){
var结果=”;
// ...
if(args.length)result+=newFunction(…args);
返回结果;
};
在ECMAScript 5中,使用:

var newFunction=function(thisArg){
var结果=”,
args=[].slice.call(参数,1);
// ...
if(args.length)result+=newFunction.apply(void 0,args);
返回结果;
};
在ECMAScript 6中,使用和

var newFunction=function(thisArg,…args){
var结果=”;
// ...
if(args.length)result+=newFunction(…args);
返回结果;
};
在ECMAScript 5中,使用:

var newFunction=function(thisArg){
var结果=”,
args=[].slice.call(参数,1);
// ...
if(args.length)result+=newFunction.apply(void 0,args);
返回结果;
};
在ECMAScript 6中,使用和

var newFunction=function(thisArg,…args){
var结果=”;
// ...
if(args.length)result+=newFunction(…args);
返回结果;
};
在ECMAScript 5中,使用:

var newFunction=function(thisArg){
var结果=”,
args=[].slice.call(参数,1);
// ...
if(args.length)result+=newFunction.apply(void 0,args);
返回结果;
};
在ECMAScript 6中,使用和

var newFunction=function(thisArg,…args){
var结果=”;
// ...
if(args.length)result+=newFunction(…args);
返回结果;
};

您将数组传递给它,因此当然会在NewFunctionRight中出现一个数组。我不知道该怎么解决这个问题。。。我会把我的问题修改得更清楚。@pointy解决了这个问题。。在我发布之前就发布了。您将数组传递到其中,因此当然会在NewFunctionRight中显示数组。我不知道该怎么解决这个问题。。。我会把我的问题修改得更清楚。@pointy解决了这个问题。。在我发布之前就发布了。您将数组传递到其中,因此当然会在NewFunctionRight中显示数组。我不知道该怎么解决这个问题。。。我会把我的问题修改得更清楚。@pointy解决了这个问题。。在我发布之前就发布了。您将数组传递到其中,因此当然会在NewFunctionRight中显示数组。我不知道该怎么解决这个问题。。。我会把我的问题修改得更清楚。@pointy解决了这个问题。。我还没来得及把它贴出来。啊,好的。这是有道理的。我需要花一点时间在我的实际代码中尝试一下。我会回来,并标记这是解决了,如果(当)它的工作!非常感谢。啊,好的。这是有道理的。我需要花一点时间在我的实际代码中尝试一下。我会回来,并标记这是解决了,如果(当)它的工作!非常感谢。啊,好的。这是有道理的。我需要花一点时间在我的实际代码中尝试一下。我会回来,并标记这是解决了,如果(当)它的工作!非常感谢。啊,好的。这是有道理的。我需要花一点时间在我的实际代码中尝试一下。我会回来,并标记这是解决了,如果(当)它的工作!非常感谢。
result += newFunction.apply(undefined, args);