0

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

6
  • A couple things bother me here... self.init() = function() {..} doesn't seem legal. self.init = function() {...} does. Next, plotPoint does not return any value, so the value of newPoint here would be... null or undefined? var newPoint = new plotPoint(self.leadPoint.y, self.firstPoint, null); Commented Sep 2, 2015 at 18:56
  • 1
    @wwwmarty: when function is invoked as a constructor (i.e., with a new), by default, it returns a new instance of itself - an object that taps into plotPoint prototype. To @MrDiggles: how about creating a fiddle with your code sample: jsfiddle.net Commented Sep 2, 2015 at 18:59
  • @DRD I added the link to the question Commented Sep 2, 2015 at 19:18
  • @MrDiggles It doesn't seem like your jsFiddle does anything when I click run (it just shows "PIXI.Container is not a function" in console). I'm using Chrome 45. Commented Sep 2, 2015 at 19:23
  • 1
    A side comment. If you are going to have several instances of Fourier(), then why not add all of its methods to its prototype? Also, in this line self.firstPoint = self.newPoint;, the self.newPoint is never declared and hence it is undefined and that could be your problem. Commented Sep 2, 2015 at 19:33

1 Answer 1

2

The object self does not have newPointproperty, hence it is undefined. When you assign undefined to self.firstPoint on line 106, then it becomes undefined also.

Sign up to request clarification or add additional context in comments.

1 Comment

D'oh. Yeah that was the problem. Thanks for the help

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.