I am trying my hand at JS and its assorted libraries and I'm running into an error I'm not quite sure how to interpret. The relevant code is as follows:
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;
}
}
All three of the console.log results show the correct object but the latter two are accompanied by an "undefined":
[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
It then throws the error "Uncaught TypeError: Cannot set property 'prev' of undefined", referring to the line shortly after [3].
If it helps, the program was working when it wasn't wrapped up in the class Fourier. I was trying to modify it so I could use it with dat.GUI.
Thanks in advance for any responses!
EDIT: link to jsfiddle
new), by default, it returns a new instance of itself - an object that taps intoplotPointprototype. To @MrDiggles: how about creating a fiddle with your code sample: jsfiddle.netFourier(), then why not add all of its methods to its prototype? Also, in this lineself.firstPoint = self.newPoint;, theself.newPointis never declared and hence it isundefinedand that could be your problem.