-1

I'm trying to do something in Javascript where I call a return function inside of another function, all within a class, it goes something like this:

class MyClass {
 constructor (x,y) {
  this.x = x;
  this.y = y;
 }

 newValues () {
  this.x = findNextXValue(this.x);
 }

 findNextXValue (x) {
  let changeVal = x + 5;
  return changeVal;
 }

}

When I try this code in p5js I get an error saying that findNextXValue is not defined. Why can't I do something like this? Any clarification would be appreciated, thanks.

  • Where do you think `findNextXValue` is defined? (It isn't defined in your example code.) – Wyck Apr 21 '22 at 00:05
  • Class methods exist on the object (or prototype), they're not standalone identifiers - put `this.` in front – CertainPerformance Apr 21 '22 at 00:05
  • I just realized that maybe I missed the problem that I am actually facing - I am trying to do an if statement within the newValues() function that says something like if (findNextXValue(this.x) > 20) { this.x = 20 } – Kiranami Apr 21 '22 at 00:15

1 Answers1

0

Missing this should be...

this.findNextXValue(this.x);

But you really don't need to pass this.x either. Just access this.x in the function.

Jackie
  • 21,969
  • 32
  • 147
  • 289