


Using Collections in JavaScript to Store and Manipulate Related Objects
Collections are a way to group related objects together. In JavaScript, you can use the `Object.defineProperty()` method to define properties on an object that are not enumerable and cannot be modified by users. These properties are called "collections" because they allow you to store and manipulate groups of related objects in a way that is similar to how you would use an array or a list.
Here is an example of how you might use collections in JavaScript:
```
const person = {
name: 'John',
age: 30,
addresses: [
{ street: '123 Main St', city: 'Anytown', state: 'CA' },
{ street: '456 Elm St', city: 'Anytown', state: 'CA' }
]
};
// The addresses property is a collection of objects
console.log(person.addresses); // Output: [{ street: '123 Main St', city: 'Anytown', state: 'CA' }, { street: '456 Elm St', city: 'Anytown', state: 'CA' }]
// You can add new addresses to the collection using the push() method
person.addresses.push({ street: '789 Oak St', city: 'Anytown', state: 'CA' });
// You can access the properties of each address object in the collection
console.log(person.addresses[0].street); // Output: '123 Main St'
console.log(person.addresses[1].city); // Output: 'Anytown'
```
In this example, the `addresses` property is a collection of objects that contain information about the person's addresses. You can use the `push()` method to add new addresses to the collection, and you can access the properties of each address object in the collection using dot notation.
Collections are useful when you need to store and manipulate groups of related objects in JavaScript. They allow you to define properties on an object that are not enumerable and cannot be modified by users, which can help you to protect the integrity of your data.



