Javascript中的链接列表和对象错误

时间:2015-09-02 18:50:26

标签: javascript

我正在尝试JS及其各种库,我遇到了一个错误,我不太清楚如何解释。相关代码如下:

function plotPoint (y, next, prev) {
    this.y = y;
    this.next = next;
    this.prev = prev;
}

function Fourier() {
    var self = this;
    self.init() = function() {
        ...
        self.firstPoint = new plotPoint(250, null, null);
        console.log(self.firstPoint); [1]
        ...
    }

    self.animate() = function() {
        ...
        console.log(self.firstPoint); [2]
        self.updatePlot();
        ...
    }

    self.updatePlot = function() {

        console.log(self.firstPoint); [3]

        //add new point to beginning
        var newPoint = new plotPoint(self.leadPoint.y, self.firstPoint, null);
        self.firstPoint.prev = newPoint
        self.firstPoint = self.newPoint;

        //remove last point from list, leave for collection
        self.lastPoint = self.lastPoint.prev;
        self.lastPoint.next = null;
    }
}

所有三个console.log结果都显示正确的对象,但后两个伴随着“未定义”:

[1] plotPoint {y: 250, next: plotPoint, prev: null}
[2] plotPoint {y: 250, next: plotPoint, prev: null}
[3] plotPoint {y: 250, next: plotPoint, prev: null}
[2] undefined
[3] undefined

然后抛出错误“Uncaught TypeError:无法设置属性'prev'of undefined”,指的是[3]之后不久的行。

如果它有帮助,那么当程序没有包含在傅里叶类中时,该程序就可以正常工作。我试图修改它,所以我可以使用它与dat.GUI。

提前感谢您的回复!

编辑:link到jsfiddle

1 个答案:

答案 0 :(得分:2)

对象self没有newPoint属性,因此未定义。当您在第106行上将undefined分配给self.firstPoint时,它也会变为undefined