Javascript Chrome中未捕获的TypeError无法读取属性';x';未定义的

Javascript Chrome中未捕获的TypeError无法读取属性';x';未定义的,javascript,jquery,html,css,google-chrome,Javascript,Jquery,Html,Css,Google Chrome,我正在尝试做一个代码来画垂直线 Chrome错误会在这一段中弹出。 未捕获的TypeError:无法读取未定义的属性“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(li

我正在尝试做一个代码来画垂直线

Chrome错误会在这一段中弹出。 未捕获的TypeError:无法读取未定义的属性“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]];
}
有人能告诉我问题出在哪里吗

此代码笔在IE中根本不起作用。

您是否正确定义了“行”?看看你的推荐信,我发现:

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;
};
您的代码中确实有这个(或类似的东西),不是吗?

您正确定义了“行”吗?看看你的推荐信,我发现:

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;
};
你的代码中确实有这个(或类似的东西),不是吗?

所以索引是这样的,所以我刚刚注意到索引--在引用的站点上弹出相同的错误。我刚刚注意到--在引用的站点上弹出相同的错误。