【问题标题】:Uncaught SyntaxError: Illegal return statement in recursive function未捕获的 SyntaxError:递归函数中的非法返回语句
【发布时间】:2020-02-09 08:45:02
【问题描述】:

我在靠近底部的代码行中收到一条非法的返回语句:返回点

createPoints(x, y, length, depth, angle, points)
{
  if(depth > 0)
  {
    //draws line
    points.push((x + length) * Math.sin(angle));
    points.push((y + length) * Math.cos(angle));

    //draw left branch
    angle += Math.PI / 4;
    createPoints(treeString, (x + length/2) * Math.sin(angle), (y + length/2) * Math.cos(angle), depth - 1, points);

    //goes back
    points.push(x);
    points.push(y);

    //draw right branch
    angle -= Math.PI / 2;
    createPoints(treeString, (x + length/2) * Math.sin(angle), (y + length/2) * Math.cos(angle), depth - 1, points);
    return points;
  }
  return;
}

该函数应该在数组中绘制点,以便在 webgl 中使用分形树。我不确定我为什么会出错,不幸的是,我的教授和助教都不知道。

【问题讨论】:

  • 由于您只能在Function 中使用return,因此您必须声明createPoints,即function createPoints(x, y, length, depth, angle, points) { // function body ... } 。检查有关函数的 MDN 链接并查看声明函数的替代方法。
  • 你能显示完整的错误(截图,如果需要的话)和更多的代码上下文吗?
  • 哦,这段代码只是在顶层吗?您正在尝试使用仅适用于类和对象字面量的简写。需要有某种声明,就像@NikKyriakides 说的那样。
  • 能否分享整个代码以便我们调试?

标签: javascript recursion return


【解决方案1】:

您的函数名称缺少函数关键字
试试下面的模式,它会帮助你。

 function createPoints () {} 

let createPoints = () => {}

【讨论】:

    猜你喜欢
    • 2016-05-16
    • 2015-10-07
    • 1970-01-01
    • 1970-01-01
    • 2021-08-31
    • 1970-01-01
    • 2015-12-22
    • 2018-06-28
    • 2010-10-30
    相关资源
    最近更新 更多