javascript - Chrome 中未捕获的类型错误无法读取未定义的属性 'x'

标签 javascript jquery html css google-chrome

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

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]];
}

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

此代码笔在 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;
};

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

关于javascript - Chrome 中未捕获的类型错误无法读取未定义的属性 'x',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38505847/

相关文章:

javascript - React.js onChange 让 parent 知道改变的状态

javascript - 如何检查点击是直接在元素中还是仅在其某些子元素中?

html - 搜索框单击位置在 css 中不正确

javascript - 将下拉列表或文本输入值保存在同一列中(使用 Laravel 5.5)

javascript - Javascript 中变量(var)的作用是什么

javascript - 如何从头开始设置 Twitter typeahead?

javascript - 在 javascript 中切换点击事件的最佳方法是什么?

javascript - JQuery Datepicker 到 PHP 变量没有形式

javascript - 获取子元素的所有 id

用于分辨率检测的 JavaScript,但似乎返回错误