# What to do about variables that are not assigned in a class?

**URL:** https://discourse.processing.org/t/what-to-do-about-variables-that-are-not-assigned-in-a-class/5517
**Category:** Coding Questions
**Created:** [November 14, 2018, 1:29am UTC](https://discourse.processing.org/t/what-to-do-about-variables-that-are-not-assigned-in-a-class/5517 "2018-11-14T01:29:52Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![yangs](https://avatars.discourse-cdn.com/v4/letter/y/8c91f0/32.png) [@yangs](https://discourse.processing.org/u/yangs)
#### Post date: [November 14, 2018, 1:29am UTC](https://discourse.processing.org/t/what-to-do-about-variables-that-are-not-assigned-in-a-class/5517/1 "2018-11-14T01:29:52Z")

</div>

Hi all! I’m trying to convert [Daniel Shiffman’s sine wave](https://p5js.org/examples/math-sine-wave.html) example to a class so I can have multiple instances of it.

However, when I start creating the class:

```auto
class Wave {
  constructor(){
    this.xspacing = 20;
    this.w = w;
    this.theta = 0.0;
    this.amplitude = 25.0;
    this.period = 500.0;
    this.dx = dx
    this.yvalues = yvalues;
  }

  calcWave(){
    this.theta += 0.2;
    var x = this.theta
    for (let i = 0; i < this.yvalues.length; i++){
      this.yvalues[i] = sin(x) * this.amplitude;
      x += this.dx;
    }
  }

  renderWave(){
    noStroke();
    fill(0, 47, 75, 127);
    for (let x = 0; x < this.yvalues.length; x++){
      ellipse(x*this.xspacing, height/2+this.yvalues[x], 66, 16)
    }
  }
}

let wave = new Wave();
const numWaves = 10
let waves = Array(numWaves)

```

I run into the problem where the variables w, dx, and yvalues, are not initially assigned to a value of some sort. Could I get some direction in how to handle this? I don’t think you would put just those in the constructor because you’re not assigning them to the instance (let wave = new Wave()).

My errors, naturally, result in ‘w is not defined’.

Thank you!

---

<div class="post-metadata">

### Author: ![kfrajer](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kfrajer/32/196_2.png) [@kfrajer](https://discourse.processing.org/u/kfrajer)
#### Post date: [November 14, 2018, 2:33am UTC](https://discourse.processing.org/t/what-to-do-about-variables-that-are-not-assigned-in-a-class/5517/2 "2018-11-14T02:33:01Z")

</div>

> [@yangs](#):
>
> for (let i = 0; i \< this.yvalues.length; i++){

Here I can see two things: yvalues is an array and it has elements so for size to return non-zero ( or even better, it will not return **not defined** ). The only way for this to work is to use one of the definitions for `Array()`: [Array - JavaScript | MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array#Syntax)

Consider:

`yvalues=new Array(arrayLength);`

Based on the link you provided, the value of _arrayLength_ should be `width/16`. You have either of two options: In the constructor you initialized your array based on this _constant_ value or you provide this value as a parameter of your constructor like this:

```auto
constructor(w){
  this.w = w
  .... OTHER lines of code
}
let wave = new Wave(width/16);

```

Which one should you implement? One is preferred over the other, only because you can adjust the size on creation. For demonstration purposes, either would work.

Kf

Kf

---

<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: [November 14, 2018, 3:42am UTC](https://discourse.processing.org/t/what-to-do-about-variables-that-are-not-assigned-in-a-class/5517/3 "2018-11-14T03:42:02Z")

</div>

> [@yangs](#):
>
> I run into the problem where the variables _w_, _dx_, and _yvalues[]_, are not initially assigned to a value of some sort.

Well, your Wave’s constructor got no declared parameters at all: `constructor() {` 😮

Just like regular functions, constructors also need parameters in order to received passed arguments into it: 🚧

> **[class - JavaScript | MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/class)**
>
> The class declaration creates a binding of a new class to a given name.

```javascript
class Wave {
  constructor(w, dx, yVals) {
    this.w = w;
    this.dx = dx;
    this.yvalues = yVals;

    this.theta = 0;
    this.xspacing = 20;
    this.amplitude = 25;
    this.period = 500;
  }
}

```

---

<div class="post-metadata">

### Author: ![Lexyth](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/lexyth/32/7403_2.png) [@Lexyth](https://discourse.processing.org/u/Lexyth)
#### Post date: [November 14, 2018, 10:32am UTC](https://discourse.processing.org/t/what-to-do-about-variables-that-are-not-assigned-in-a-class/5517/4 "2018-11-14T10:32:44Z")

</div>

I‘m not sure why noone said this, But your class doesn‘t have any class intern variables that it could set with this.w. You Need to add them (preferably) just before the constructor. Something like :

```auto
class Wave {
w = 0;
xSpace = 0;

  Wave(int w_, int xSpace_) {
    this.w = w_;
    this.xSpace = xSpace_;
  }
}

```

Note, since noone said this before, i‘m not all too sure…

---

<div class="post-metadata">

### Author: ![yangs](https://avatars.discourse-cdn.com/v4/letter/y/8c91f0/32.png) [@yangs](https://discourse.processing.org/u/yangs)
#### Post date: [November 14, 2018, 7:04pm UTC](https://discourse.processing.org/t/what-to-do-about-variables-that-are-not-assigned-in-a-class/5517/5 "2018-11-14T19:04:31Z")

</div>

Thank you everyone, really appreciate the direction!

It leads me to another question, then, of the [Jitter example](https://p5js.org/examples/objects-objects.html) that uses a class (albeit, declared with function). My confusion stems from the fact that there is no assignment of those parameters to a constructor so how is this example different to my first attempt at not declaring anything in the constructor?

Thanks!

---

<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: [November 14, 2018, 8:21pm UTC](https://discourse.processing.org/t/what-to-do-about-variables-that-are-not-assigned-in-a-class/5517/6 "2018-11-14T20:21:29Z")

</div>

```auto
// Jitter class
function Jitter() {
  this.x = random(width);
  this.y = random(height);
  this.diameter = random(10, 30);
  this.speed = 1;

```

All of those 4 properties above are initialized either by a **random()** value or a literal number (1).

Your own Wave’s **constructor()** got 3 properties initialized by unknown variables _w_, _d_ & _yvalues_:

> [@yangs](#):
>
> ```auto
> class Wave {
> constructor(){
> this.xspacing = 20;
> this.w = w;
> this.theta = 0.0;
> this.amplitude = 25.0;
> this.period = 500.0;
> this.dx = dx
> this.yvalues = yvalues;
> }
> 
> ```

---

<div class="post-metadata">

### Author: ![yangs](https://avatars.discourse-cdn.com/v4/letter/y/8c91f0/32.png) [@yangs](https://discourse.processing.org/u/yangs)
#### Post date: [November 16, 2018, 2:56am UTC](https://discourse.processing.org/t/what-to-do-about-variables-that-are-not-assigned-in-a-class/5517/7 "2018-11-16T02:56:18Z")

</div>

Thank you, that gives me some insight!
