繁体   English   中英

为什么在调用Line构造函数时某些值被初始化为NaN?

[英]Why when a Line constructor is called are some of the values being initialized as NaN?

我正在调用带有两个参数(两个点)的Line构造函数,即开始和结束。 点具有x和y属性。 这两个构造函数大部分时间都在工作(始终指向Point),但是当我调用Line.getPerpendicular(返回新的Line)时,即使将正确的值传递给Line构造函数,其端点值也都是NaN 。

我在Line构造函数中隔离了点,并且端点最初具有值,但是当我设置this.end = endPoint时,this.end是x和y的NaN。

直接创建点而不是先创建点不会有帮助。

Line构造函数大部分时间都在工作。

function line(start, end) {
  console.log({ start, end });
  // { start: point { x: 97, y: 299 }, end: point { x: 223, y: 341 } }
  /* 
  this.start = start;
  this.end = end;
  Originally I thought this might be the problem, like I was
  passing in end by reference and then it was being gc'd
  */
  const ex = end.x;
  const ey = end.y;
  const endP = Point(ex, ey);
  console.log("END POINT", endP); // point { x: 223, y: 341 }
                                  // the Point is definitely constructed
                                  // correctly.
  this.start = Point(start.x, start.y);
  this.end = endP;

  console.log(this);
  // line { start: point { x: 97, y: 299 }, end: point { x: NaN, y: NaN } }

}

您可以通过以下方法进行复制克隆https://github.com/Sjbrimley26/geometry ,运行npm i,然后运行npm build。 然后,只需打开内置的html文件并检出控制台即可。

//编辑这是getPerpendicular函数,显然会导致该错误,因为Line构造函数会以其他方式工作,尽管我不明白为什么,因为Line是用正确的点值调用的。

line.prototype.getPerpendicular = function() {
  const { slope, length, center } = this;
  const inv = -1 * divide(1)(slope);
  const { x, y } = center;
  const b = subtract(y)(inv * x);

  const x0 = x - Math.floor(length);
  const y0 = x0 * inv + b;
  const x1 = x + Math.floor(length);
  const y1 = x1 * inv + b;
  console.log({ x0, y0, x1, y1 });
  const start = Point(x0, y0);
  const end = Point(x1, y1);
  console.log({ start, end });
  return Line(start, end);

 /*
  This doesn't work either
  return Line(
    Point(
      x - Math.floor(length), 
      (x- Math.floor(length)) * inv + b
    ),
    Point(
      x + Math.floor(length),
      (x + Math.floor(length)) * inv + b
    )
  );
  */
}

这是什么语言?

您是否检查过如果从3行中删除const会发生什么? 不懂这种语言,我在这里处于劣势。

我假设这是一种无需声明函数参数的语言? 在许多语言中,标头应为功能行(Point start,Point end)。

为什么不以与this.start相同的方式初始化this.end? 那就是:this.end = Point(end.x,end.y);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM