【问题标题】:Uncaught TypeError in Chrome Cannot read property 'x' of undefinedChrome 中未捕获的 TypeError 无法读取未定义的属性“x”
【发布时间】:2016-07-21 13:37:41
【问题描述】:

我正在尝试编写代码来绘制垂直线。

http://codepen.io/juyoung518/pen/BzAJwg

此段落会弹出 Chrome 错误。 未捕获的类型错误:无法读取未定义的属性“x”

function getNearestIntersections(line, arr) {
    var index = arr.length - 2;
    if (arr[index].x !== line.center.x) {
        while (Math.sign(line.center.x - line.a.x) === Math.sign(line.center.x - arr[index].x)) {
            index--;
        }
    } else {
        while (Math.sign(line.center.y - line.a.y) === Math.sign(line.center.y - arr[index].y)) {
            index--;
        }
    }

    return [arr[arr.length - 1], arr[index]];
}

谁能告诉我问题出在哪里?

这个 codepen 在 IE 中根本不起作用。

【问题讨论】:

  • 所以索引是

标签: javascript jquery html css google-chrome


【解决方案1】:

您是否正确定义了“线”?看着你的参考,我看到了:

var Line = function(x1, y1, x2, y2) {
    this.a = new Vec(x1, y1);
    this.b = new Vec(x2, y2);
    this.center = new Vec((x1 + x2) / 2, (y1 + y2) / 2);
    this.dx = x2 - x1;
    this.dy = y2 - y1;
};

你的代码中确实有这个(或类似的东西),不是吗?

【讨论】:

  • 我刚刚注意到——在引用的网站上弹出了同样的错误。
猜你喜欢
  • 2023-01-14
  • 2022-09-22
  • 1970-01-01
  • 2022-12-21
  • 2019-05-16
  • 2018-05-28
  • 1970-01-01
  • 1970-01-01
  • 2022-12-21
相关资源
最近更新 更多