Individualize functions in P5.js

Hi @Kevincroos,

First of all one thing about your code:

You shouldn’t put the drawBall() function (and others) inside the draw() function because it’s not optimized and it’s not needed in the current configuration since the function will always be the same. As stated in the following website:

Unlike other types of values, functions typically don’t change; a function is created to perform a specific task. So it doesn’t make much sense to waste CPU cycles recreating a somewhat static value over and over again.

Aah! this is exactly the purpose of Object-oriented programming: being able to represent your data as units of data (called objects).

Check a previous thread I did on the subject (with balls but in Java, it shouldn’t be too hard to do it in JS):

Also see MDN’s documentation on the subject:

2 Likes