1

in an example given on the p5 Site a parameter (I think that's the correct term) is used 'function(other)' which I can't quite understand. I appreciate you will need the full code which is available from the above link. Anyone care to explain would be much appreciated.

Particle.prototype.display = function(other) {
  stroke(0, this.lifespan);
  fill(0, this.lifespan/2);    
  ellipse(this.position.x,this.position.y, 8, 8);    
  // If we need to draw a line
  if (other) {
    line(this.position.x, this.position.y, other.position.x, other.position.y);
  }
}
James
  • 1,355
  • 3
  • 14
  • 38

1 Answers1

1

A function can take a parameter. The parameter is writen in the brackets. In this example the paramether is other. In the example above. "other" is another object. E.g. Particle.prototype.display(someobject);

I hope this is somewhat understandable.

var div = document.getElementById("test");

test(true);
test(false);

function test(parameter){
  if(parameter){
    div.innerHTML += "Hello"
    }else{
    div.innerHTML += " World!"
    }
}
<div id="test"></div>
Nicolo Lüscher
  • 595
  • 7
  • 22