# Nested function?

**URL:** <https://discourse.processing.org/t/nested-function/20639>\
**Category:** Beginners\
**Created:** [May 8, 2020, 5:45pm UTC](https://discourse.processing.org/t/nested-function/20639 "2020-05-08T17:45:10Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![noel](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/noel/32/213_2.png) [@noel](https://discourse.processing.org/u/noel)\
**Post date:** [May 8, 2020, 5:45pm UTC](https://discourse.processing.org/t/nested-function/20639/1 "2020-05-08T17:45:10Z")

</div>

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?

```auto
let particles = [];

function draw() {
  for (let p of particles) {
    p.draw();
    p.move();
  }
	if (frameCount % 20 === 0) {
		updateParticles();
	}
}

```

```auto
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);
  }
}

```

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [May 8, 2020, 5:59pm UTC](https://discourse.processing.org/t/nested-function/20639/2 "2020-05-08T17:59:24Z")

</div>

- 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:

```auto
Particle.prototype.move = function () {
  // ...
}

Particle.prototype.draw = function () {
  // ...
}

```

- The modern way of implementing a `class` on JS is like this now:

> **[Classes](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes)**
>
> JavaScript classes, introduced in ECMAScript 2015, are primarily syntactical sugar over JavaScript's existing prototype-based inheritance. The class syntax does not introduce a new object-oriented inheritance model to JavaScript.
