Nested function?

I am making my first steps in P5.js, and trying to translate some js to java code, but I don’t understand the function below. It seems some nested function. How can I translate this?

let particles = [];

function draw() {
  for (let p of particles) {
    p.draw();
    p.move();
  }
	if (frameCount % 20 === 0) {
		updateParticles();
	}
}
function Particle(x_, y_, s_, c_) {
  this.x = x_;
  this.y = y_;
  this.size = s_;
  this.c = c_;
	
  this.dist = 1;
  
  this.move = function() {
    let theta = noise(this.x * sness,
                      this.y * sness)*PI*4;
    let v = p5.Vector.fromAngle(theta, this.dist);
    this.x += v.x;
    this.y += v.y;
    //this.dist *= 0.9;
	  //this.size *= 0.99;
  }
  
  this.draw = function() {
    fill(this.c);
    circle(this.x, this.y, this.size);
  }
}
1 Like
  • That is an outdated way of creating a class on JS before ES6 (ES2015) has arrived.
  • Not only old, but made in a way which wastes RAM!
  • B/c every time a Particle is instantiated, all of its methods are recreated as well!
  • Instead, its methods move() & draw() should be implemented outside Particle’s constructor function by adding them to its prototype{} object like this:
Particle.prototype.move = function () {
  // ...
}

Particle.prototype.draw = function () {
  // ...
}
  • The modern way of implementing a class on JS is like this now:
2 Likes