<aside> 🧟 This page is still a draft!
</aside>
If you use an Object Oriented Programming approach in your code, it’s important to know that while you can share simple data objects with p5.party, sharing OOP objects won’t work. A full introduction to Javascript OOP is out of scope for these documents, but here is a quick overview of Objects, OOP, and why OOP objects don’t work with p5.party.
In Javascript, an object is a collection of properties. Each property has a name (also called a key) and a value. This code declares a variable named myDoor
with two properties: isOpen
and isLocked
.
let myDoor = {
isOpen: false,
isLocked: true
}
Both isLocked
and isOpen
are boolean values, but properties can be other types including strings, numbers, arrays, and even nested objects.
let myDoor = {
isOpen: false,
isLocked: true,
position: {
x: 10,
y: 5
}
}
Object Oriented Programing is a programming approach that uses objects to organize data and functionality. JavaScript object properties can also be functions. This allows you to use an object to hold both data and the functions that operate on that data.
let myDoor = {
isOpen: false,
isLocked: false,
open: ()=>{
if (this.isLocked) {
console.log("The door cannot be opened when locked.");
} else {
this.isOpen = true;
}
}
}
In the example above a new property called open
has been added to the object, myDoor.open
is a function. You would call it like this: myDoor.open()
.
You can share null, booleans, numbers, strings, arrays, and (data) objects as properties on a p5.party shared object.
You can not share functions. If you assign a function to a shared object property, it will be available locally (for now, future versions might strip them even locally), but won’t be synced.
If you try to share an object with both data properties and function properties (methods), the methods will be removed, leaving only the data properties.