Rhythm Game Example
// Set up an immutable object to represent a beat in the game
const beat = Object.freeze({
sound: 'kick', // The sound that will be played for this beat
time: 1000 // The time in milliseconds when the beat occurs
});
// Create a copy of the beat object and update the time property
const updatedBeat = { ...beat, time: 1500 };
// Create a new array of beats for the game
const beats = [beat, updatedBeat];
// Add a new beat to the array
const newBeat = { ...beat, sound: 'snare', time: 2000 };
beats.push(newBeat);
// Delete a beat from the array
beats.splice(1, 1);
console.log(beats); // [ { sound: 'kick', time: 1000 }, { sound: 'snare', time: 2000 } ]Last updated