Introduction to object oriented programming #
Object oriented programming (OOP) describe programs as a group of mutually interactive objects storing user-defined attributes (data or fields), and methods to manipulate them. The set of public attributes and methods defines the object Application Programming Interface (API). OOP always involves three steps: 1. Object declaration; 2. Object initialization; and, 3. Object usage.
Consider the following p5 example taken as is from the createColorPicker API documentation.
colorPicker
// 1. Object declaration
let colorPicker;
function setup() {
createCanvas(100, 100);
// 2. Object initialization
colorPicker = createColorPicker('#ed225d');
// 3. Object usage
colorPicker.position(0, height + 5);
}
function draw() {
// 3. Object usage
background(colorPicker.color());
}
Observations
- Object initialization can be achieved either through object literals, a p5 method call (like the above example or when creating a quadrille object), or class constructors.
- Object usage is always done using the dot syntax.