gpt4 book ai didi

javascript - Javascript 中的链表和对象错误

转载 作者:行者123 更新时间:2023-12-02 15:44:09 27 4
gpt4 key购买 nike

我正在尝试 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: Cannot set property 'prev' of undefined”,引用 [3] 之后不久的行。

如果有帮助的话,该程序在没有包含在傅里叶类中时正在运行。我试图修改它,以便可以将它与 dat.GUI 一起使用。

预先感谢您的回复!

编辑:link到jsfiddle

最佳答案

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

关于javascript - Javascript 中的链表和对象错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32360933/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com