Share
Explore

Example of IIFE in Guest Entertainment Services at Cancun Resort


Example of IIFE in Guest Entertainment Services at Cancun Resort
In this example, we'll use an Immediately Invoked Function Expression (IIFE) to simulate a module for managing guest entertainment services at a resort.
The IIFE will encapsulate the functionality, ensuring that the internal state and variables are private and not accessible from the outside.

const CancunResortEntertainment = (function() { // Private variables and functions const events = [ { name: 'Beach Volleyball', time: '10:00 AM' }, { name: 'Snorkeling', time: '11:00 AM' }, { name: 'Dance Class', time: '1:00 PM' }, { name: 'Cooking Class', time: '3:00 PM' }, ]; let guests = [];
function addGuest(name) { guests.push(name); console.log(`${name} has been added to the guest list.`); }
function scheduleEvent(event) { console.log(`Scheduling event: ${event.name} at ${event.time}`); }
function listEvents() { console.log('Today\'s Events:'); events.forEach(event => console.log(`${event.name} at ${event.time}`)); }
function listGuests() { console.log('Current Guests:'); guests.forEach(guest => console.log(guest)); }
// Public API return { addGuest, scheduleEvent, listEvents, listGuests, }; })();
// Using the module CancunResortEntertainment.addGuest('John Doe'); CancunResortEntertainment.addGuest('Jane Smith'); CancunResortEntertainment.listGuests();
CancunResortEntertainment.scheduleEvent({ name: 'Pool Party', time: '4:00 PM' }); CancunResortEntertainment.listEvents();
### Explanation
1. **IIFE Definition**: The IIFE is defined and immediately executed, encapsulating the variables and functions within its scope.
This ensures that the internal state (such as the list of events and guests) is private and cannot be accessed directly from outside the IIFE. const CancunResortEntertainment = (function() { // Private variables and functions const events = [ { name: 'Beach Volleyball', time: '10:00 AM' }, { name: 'Snorkeling', time: '11:00 AM' }, { name: 'Dance Class', time: '1:00 PM' }, { name: 'Cooking Class', time: '3:00 PM' }, ]; let guests = [];
function addGuest(name) { guests.push(name); console.log(`${name} has been added to the guest list.`); }
function scheduleEvent(event) { console.log(`Scheduling event: ${event.name} at ${event.time}`); }
function listEvents() { console.log('Today\'s Events:'); events.forEach(event => console.log(`${event.name} at ${event.time}`)); }
function listGuests() { console.log('Current Guests:'); guests.forEach(guest => console.log(guest)); }
// Public API return { addGuest, scheduleEvent, listEvents, listGuests, }; })(); ```
2. **Private Variables and Functions**:
Within the IIFE, `events` and `guests` arrays are defined as private variables.
Functions like `addGuest`, `scheduleEvent`, `listEvents`, and `listGuests` are also defined inside the IIFE to manipulate these private variables. ```javascript const events = [ { name: 'Beach Volleyball', time: '10:00 AM' }, { name: 'Snorkeling', time: '11:00 AM' }, { name: 'Dance Class', time: '1:00 PM' }, { name: 'Cooking Class', time: '3:00 PM' }, ]; let guests = [];
function addGuest(name) { guests.push(name); console.log(`${name} has been added to the guest list.`); }
function scheduleEvent(event) { console.log(`Scheduling event: ${event.name} at ${event.time}`); }
function listEvents() { console.log('Today\'s Events:'); events.forEach(event => console.log(`${event.name} at ${event.time}`)); }
function listGuests() { console.log('Current Guests:'); guests.forEach(guest => console.log(guest)); } ```
3. **Public API**: The IIFE returns an object that exposes certain functions (`addGuest`, `scheduleEvent`, `listEvents`, `listGuests`) as a public API, while keeping the rest of the code encapsulated. ```javascript return { addGuest, scheduleEvent, listEvents, listGuests, }; ```
4. Using the Module:
The module is used by calling its methods through the `CancunResortEntertainment` object.
This ensures that the internal state is managed properly and that external code interacts with the module only through the provided API. CancunResortEntertainment.addGuest('John Doe'); CancunResortEntertainment.addGuest('Jane Smith'); CancunResortEntertainment.listGuests();
CancunResortEntertainment.scheduleEvent({ name: 'Pool Party', time: '4:00 PM' }); CancunResortEntertainment.listEvents(); ```
### Output
The output will be: ``` John Doe has been added to the guest list. Jane Smith has been added to the guest list. Current Guests: John Doe Jane Smith Scheduling event: Pool Party at 4:00 PM Today's Events: Beach Volleyball at 10:00 AM Snorkeling at 11:00 AM Dance Class at 1:00 PM Cooking Class at 3:00 PM ```
### Explanation
- **Private Scope**: The IIFE encapsulates the internal logic, ensuring that `events` and `guests` are not accessible from outside the module, maintaining a clean and controlled environment. - **Public Methods**: Only the methods defined in the returned object (`addGuest`, `scheduleEvent`, `listEvents`, `listGuests`) are accessible, providing a clear interface for interacting with the module.
This example demonstrates how IIFEs can be used to manage private state and create modular, maintainable code in a complex application, such as managing guest entertainment services at a resort.
Want to print your doc?
This is not the way.
Try clicking the ⋯ next to your doc name or using a keyboard shortcut (
CtrlP
) instead.