JavaScript
Wat zijn objects en hoe werk je ermee?
Objects zijn key-value collecties.
Object structuur
const obj = { key: value, ... } Access: obj.key of obj['key'] Muteren: obj.newKey = value
Methods vs Properties
Properties = data (strings, numbers) Methods = functies in object
Code Voorbeelden
JAVASCRIPTObjects
const person = {
name: "Jan", // Property
age: 30,
city: "Amsterdam",
// Method
greet() {
console.log("Hallo, ik ben " + this.name);
},
// Method 2
getBio: function() {
return `${this.name}, ${this.age} jaar`;
}
};
// Access properties
console.log(person.name); // "Jan"
console.log(person["age"]); // 30
// Call methods
person.greet(); // "Hallo, ik ben Jan"
console.log(person.getBio()); // "Jan, 30 jaar"
// Destructuring
const { name, age } = person;
console.log(name); // "Jan"
// Spread operator
const newPerson = { ...person, age: 31, city: "Rotterdam" };Relevante trefwoorden
objectmethodspropertiesdestructuring