javascript

examples

examples.js⚔
// ╔══════════════════════════════════════════════════════════════════════════════╗
// ā•‘                         COMMONJS & AMD MODULES                                ā•‘
// ā•‘                 Legacy Module Systems for Node.js & Browsers                  ā•‘
// ā•šā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•

/*
ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”
│                        MODULE SYSTEMS COMPARISON                                 │
ā”œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”¤
│                                                                                  │
│  ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”¬ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”¬ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”¬ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”  │
│  │                │  CommonJS       │  AMD            │  ES Modules         │  │
│  ā”œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”¼ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”¼ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”¼ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”¤  │
│  │  Environment   │  Node.js        │  Browser        │  Universal          │  │
│  │  Loading       │  Synchronous    │  Asynchronous   │  Async (static)     │  │
│  │  Syntax        │  require/       │  define/require │  import/export      │  │
│  │                │  module.exports │                 │                     │  │
│  │  When Created  │  2009           │  2010           │  2015 (ES6)         │  │
│  │  Dynamic       │  Yes            │  Yes            │  Via import()       │  │
│  │  Tree-shaking  │  No             │  No             │  Yes                │  │
│  ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”“ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”“ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”“ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜  │
│                                                                                  │
│  CommonJS: Server-side (Node.js) - synchronous loading                         │
│  AMD: Browser-side - asynchronous loading with RequireJS                        │
│  ES Modules: Modern standard - both environments                                │
│                                                                                  │
ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜
*/


// ════════════════════════════════════════════════════════════════════════════════
// PART 1: COMMONJS MODULES
// ════════════════════════════════════════════════════════════════════════════════

/*
ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”
│                           COMMONJS OVERVIEW                                      │
ā”œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”¤
│                                                                                  │
│  CommonJS = The original Node.js module system                                  │
│                                                                                  │
│  ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”   │
│  │                                                                         │   │
│  │    math.js                            main.js                           │   │
│  │    ────────                           ───────                           │   │
│  │                                                                         │   │
│  │    // Export                          // Import                         │   │
│  │    module.exports = {                 const math = require('./math');   │   │
│  │        add: (a,b) => a + b           math.add(2, 3);  // 5              │   │
│  │    };                                                                   │   │
│  │                                                                         │   │
│  ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜   │
│                                                                                  │
│  Key Characteristics:                                                           │
│  • Synchronous loading (blocking)                                               │
│  • module.exports = value to export                                             │
│  • require(path) to import                                                      │
│  • Modules cached after first load                                              │
│  • Still widely used in Node.js                                                 │
│                                                                                  │
ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜
*/


// ═══════════════════════════════════════════════════════════════════════════════
// 1.1 BASIC COMMONJS EXPORTS
// ═══════════════════════════════════════════════════════════════════════════════

/*
ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”
│                         COMMONJS EXPORT PATTERNS                                 │
ā”œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”¤
│                                                                                  │
│  Pattern 1: module.exports = object                                             │
│  Pattern 2: module.exports = function                                           │
│  Pattern 3: exports.name = value (shorthand)                                    │
│  Pattern 4: module.exports = class                                              │
│                                                                                  │
ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜
*/

// Simulating CommonJS module system
const CommonJSSimulator = (function() {
    const moduleCache = new Map();
    
    return {
        // Simulated require function
        require(moduleFactory) {
            // In real CommonJS, this would:
            // 1. Resolve the path
            // 2. Check cache
            // 3. Load and execute module
            // 4. Return exports
            
            const module = { exports: {} };
            const exports = module.exports;
            
            // Execute module factory
            moduleFactory(module, exports);
            
            return module.exports;
        }
    };
})();

// ─── Pattern 1: Exporting an Object ────────────────────────────────────────────

// File: math.js
const mathModule = CommonJSSimulator.require((module, exports) => {
    // Using module.exports for the entire object
    module.exports = {
        PI: 3.14159,
        E: 2.71828,
        
        add(a, b) {
            return a + b;
        },
        
        subtract(a, b) {
            return a - b;
        },
        
        multiply(a, b) {
            return a * b;
        },
        
        divide(a, b) {
            if (b === 0) throw new Error('Division by zero');
            return a / b;
        },
        
        power(base, exp) {
            return Math.pow(base, exp);
        }
    };
});

console.log('CommonJS Pattern 1 - Export Object:');
console.log('  math.add(5, 3):', mathModule.add(5, 3));
console.log('  math.PI:', mathModule.PI);
console.log('');

// ─── Pattern 2: Exporting a Function ───────────────────────────────────────────

// File: greet.js
const greetModule = CommonJSSimulator.require((module, exports) => {
    // Export a single function
    module.exports = function greet(name) {
        return `Hello, ${name}!`;
    };
    
    // Add properties to the function
    module.exports.formal = function(name) {
        return `Good day, ${name}.`;
    };
    
    module.exports.casual = function(name) {
        return `Hey ${name}!`;
    };
});

console.log('CommonJS Pattern 2 - Export Function:');
console.log('  greet("World"):', greetModule('World'));
console.log('  greet.formal("Sir"):', greetModule.formal('Sir'));
console.log('');

// ─── Pattern 3: Using exports Shorthand ────────────────────────────────────────

// File: utils.js
const utilsModule = CommonJSSimulator.require((module, exports) => {
    // exports is a reference to module.exports
    // Can add properties directly
    exports.capitalize = function(str) {
        return str.charAt(0).toUpperCase() + str.slice(1);
    };
    
    exports.reverse = function(str) {
        return str.split('').reverse().join('');
    };
    
    exports.truncate = function(str, length) {
        if (str.length <= length) return str;
        return str.slice(0, length) + '...';
    };
    
    // WARNING: Don't reassign exports directly!
    // exports = { something }  // This BREAKS the reference!
});

console.log('CommonJS Pattern 3 - exports Shorthand:');
console.log('  utils.capitalize("hello"):', utilsModule.capitalize('hello'));
console.log('  utils.reverse("hello"):', utilsModule.reverse('hello'));
console.log('');

// ─── Pattern 4: Exporting a Class ──────────────────────────────────────────────

// File: Logger.js
const LoggerModule = CommonJSSimulator.require((module, exports) => {
    class Logger {
        constructor(prefix = '') {
            this.prefix = prefix;
        }
        
        log(message) {
            console.log(`${this.prefix ? `[${this.prefix}] ` : ''}${message}`);
        }
        
        error(message) {
            console.error(`${this.prefix ? `[${this.prefix}] ` : ''}ERROR: ${message}`);
        }
        
        warn(message) {
            console.warn(`${this.prefix ? `[${this.prefix}] ` : ''}WARN: ${message}`);
        }
    }
    
    // Export the class itself
    module.exports = Logger;
});

console.log('CommonJS Pattern 4 - Export Class:');
const logger = new LoggerModule('App');
logger.log('Application started');
console.log('');


// ═══════════════════════════════════════════════════════════════════════════════
// 1.2 COMMONJS REQUIRE PATTERNS
// ═══════════════════════════════════════════════════════════════════════════════

/*
ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”
│                         COMMONJS REQUIRE PATTERNS                                │
ā”œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”¤
│                                                                                  │
│  // Basic require                                                               │
│  const module = require('./module');                                            │
│                                                                                  │
│  // Destructuring require                                                       │
│  const { add, subtract } = require('./math');                                   │
│                                                                                  │
│  // Core modules                                                                │
│  const fs = require('fs');                                                      │
│  const path = require('path');                                                  │
│                                                                                  │
│  // npm packages                                                                │
│  const express = require('express');                                            │
│                                                                                  │
│  // Conditional require                                                         │
│  if (condition) {                                                               │
│      const optional = require('./optional');                                    │
│  }                                                                              │
│                                                                                  │
ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜
*/

// Simulated require patterns demonstration
console.log('CommonJS Require Patterns:');

// Pattern: Destructuring from required module
const mathOps = CommonJSSimulator.require((module) => {
    module.exports = {
        add: (a, b) => a + b,
        subtract: (a, b) => a - b,
        multiply: (a, b) => a * b
    };
});

// In real CommonJS:
// const { add, subtract } = require('./math');
const { add, subtract } = mathOps;
console.log('  Destructured add(10, 5):', add(10, 5));
console.log('');


// ═══════════════════════════════════════════════════════════════════════════════
// 1.3 MODULE.EXPORTS VS EXPORTS
// ═══════════════════════════════════════════════════════════════════════════════

/*
ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”
│                      module.exports vs exports                                   │
ā”œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”¤
│                                                                                  │
│  ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”   │
│  │                                                                         │   │
│  │  // Initially:                                                          │   │
│  │  exports = module.exports = {}                                          │   │
│  │                                                                         │   │
│  │  // Both reference the same object                                      │   │
│  │  ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”                                                        │   │
│  │  │ exports ────┼───────┐                                                │   │
│  │  ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜       │      ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”                            │   │
│  │                        ā”œā”€ā”€ā”€ā”€ā”€ā–ŗā”‚     {}     │                            │   │
│  │  ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”   │      ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜                            │   │
│  │  │ module.exports ā”€ā”¼ā”€ā”€ā”€ā”˜                                                │   │
│  │  ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜                                                    │   │
│  │                                                                         │   │
│  ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜   │
│                                                                                  │
│  RULE: What you require() is ALWAYS module.exports                              │
│                                                                                  │
│  āœ“ exports.foo = 'bar'           // Works (adds to same object)                │
│  āœ“ module.exports.foo = 'bar'    // Works                                       │
│  āœ“ module.exports = { foo }      // Works (replaces the object)                 │
│  āœ— exports = { foo }             // BREAKS! (detaches from module.exports)      │
│                                                                                  │
ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜
*/

console.log('module.exports vs exports:');

// CORRECT: Adding to exports
const correctModule1 = CommonJSSimulator.require((module, exports) => {
    exports.a = 1;
    exports.b = 2;
    // Both work because exports still references module.exports
});
console.log('  Correct (exports.x = y):', correctModule1);

// CORRECT: Replacing module.exports
const correctModule2 = CommonJSSimulator.require((module, exports) => {
    module.exports = { x: 10, y: 20 };
});
console.log('  Correct (module.exports = {}):', correctModule2);

// INCORRECT: Reassigning exports (BROKEN!)
const brokenModule = CommonJSSimulator.require((module, exports) => {
    exports = { broken: true };  // This breaks the reference!
    // exports no longer points to module.exports
});
console.log('  Broken (exports = {}):', brokenModule, '← Empty because exports was detached');
console.log('');


// ═══════════════════════════════════════════════════════════════════════════════
// 1.4 COMMONJS MODULE CACHING
// ═══════════════════════════════════════════════════════════════════════════════

/*
ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”
│                         MODULE CACHING (SINGLETON)                               │
ā”œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”¤
│                                                                                  │
│  Modules are cached after first load!                                           │
│                                                                                  │
│  ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”   │
│  │                                                                         │   │
│  │  // counter.js                                                          │   │
│  │  let count = 0;                                                         │   │
│  │  module.exports = {                                                     │   │
│  │      increment() { count++; },                                          │   │
│  │      getCount() { return count; }                                       │   │
│  │  };                                                                     │   │
│  │                                                                         │   │
│  │  // file1.js                                                            │   │
│  │  const counter = require('./counter');                                  │   │
│  │  counter.increment(); // count = 1                                      │   │
│  │                                                                         │   │
│  │  // file2.js                                                            │   │
│  │  const counter = require('./counter'); // Same instance!                │   │
│  │  console.log(counter.getCount()); // 1 (not 0!)                         │   │
│  │                                                                         │   │
│  ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜   │
│                                                                                  │
│  The cache can be accessed via: require.cache                                   │
│  Clear cache: delete require.cache[require.resolve('./module')]                 │
│                                                                                  │
ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜
*/

// Simulating module caching
const ModuleSystem = (function() {
    const cache = new Map();
    
    return {
        require(name, factory) {
            // Check cache first
            if (cache.has(name)) {
                console.log(`  [Cache Hit] ${name}`);
                return cache.get(name);
            }
            
            console.log(`  [Loading] ${name}`);
            const module = { exports: {} };
            factory(module);
            
            // Cache the exports
            cache.set(name, module.exports);
            return module.exports;
        },
        
        clearCache(name) {
            cache.delete(name);
        }
    };
})();

console.log('Module Caching Demonstration:');

// Factory for counter module
const counterFactory = (module) => {
    let count = 0;
    module.exports = {
        increment() { count++; },
        decrement() { count--; },
        getCount() { return count; }
    };
};

// First require - loads the module
const counter1 = ModuleSystem.require('counter', counterFactory);
counter1.increment();
counter1.increment();
console.log('  After 2 increments:', counter1.getCount());

// Second require - returns cached instance
const counter2 = ModuleSystem.require('counter', counterFactory);
console.log('  New require, same count:', counter2.getCount(), '(same instance!)');
console.log('');


// ════════════════════════════════════════════════════════════════════════════════
// PART 2: AMD (ASYNCHRONOUS MODULE DEFINITION)
// ════════════════════════════════════════════════════════════════════════════════

/*
ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”
│                              AMD OVERVIEW                                        │
ā”œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”¤
│                                                                                  │
│  AMD = Designed for browsers with asynchronous loading                          │
│  Implemented by: RequireJS, curl.js, Dojo                                       │
│                                                                                  │
│  ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”   │
│  │                                                                         │   │
│  │  // Define a module                                                     │   │
│  │  define('moduleName', ['dep1', 'dep2'], function(dep1, dep2) {          │   │
│  │      return {                                                           │   │
│  │          // module contents                                             │   │
│  │      };                                                                 │   │
│  │  });                                                                    │   │
│  │                                                                         │   │
│  │  // Require modules                                                     │   │
│  │  require(['module1', 'module2'], function(mod1, mod2) {                 │   │
│  │      // Use modules                                                     │   │
│  │  });                                                                    │   │
│  │                                                                         │   │
│  ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜   │
│                                                                                  │
│  Key Characteristics:                                                           │
│  • Asynchronous loading (non-blocking)                                          │
│  • Designed for browsers                                                        │
│  • Dependencies declared upfront                                                │
│  • Callback receives resolved dependencies                                      │
│                                                                                  │
ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜
*/


// ═══════════════════════════════════════════════════════════════════════════════
// 2.1 AMD MODULE DEFINITION
// ═══════════════════════════════════════════════════════════════════════════════

/*
ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”
│                          AMD DEFINE PATTERNS                                     │
ā”œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”¤
│                                                                                  │
│  Pattern 1: Named module with dependencies                                      │
│  define('name', ['dep1', 'dep2'], function(dep1, dep2) { ... })                │
│                                                                                  │
│  Pattern 2: Anonymous module with dependencies                                  │
│  define(['dep1'], function(dep1) { ... })                                       │
│                                                                                  │
│  Pattern 3: Simple module (no dependencies)                                     │
│  define(function() { ... })                                                     │
│                                                                                  │
│  Pattern 4: Object literal (no dependencies, static)                            │
│  define({ key: 'value' })                                                       │
│                                                                                  │
ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜
*/

// Simulating AMD module system
const AMD = (function() {
    const modules = new Map();
    const pending = new Map();
    
    function define(name, deps, factory) {
        // Handle different call signatures
        if (typeof name !== 'string') {
            factory = deps;
            deps = name;
            name = 'anonymous_' + Math.random().toString(36).slice(2);
        }
        if (!Array.isArray(deps)) {
            factory = deps;
            deps = [];
        }
        
        // If factory is an object, wrap it
        if (typeof factory !== 'function') {
            const obj = factory;
            factory = () => obj;
        }
        
        // Store module definition
        modules.set(name, { deps, factory, resolved: null });
        console.log(`  [AMD] Defined module: ${name}`);
        
        return name;
    }
    
    function require(deps, callback) {
        if (typeof deps === 'string') {
            deps = [deps];
        }
        
        // Resolve all dependencies
        const resolved = deps.map(dep => {
            const module = modules.get(dep);
            if (!module) {
                throw new Error(`Module not found: ${dep}`);
            }
            
            if (!module.resolved) {
                // Resolve the module's dependencies first
                const depsResolved = module.deps.map(d => {
                    const m = modules.get(d);
                    return m ? m.resolved || m.factory() : undefined;
                });
                module.resolved = module.factory(...depsResolved);
            }
            
            return module.resolved;
        });
        
        // Call the callback with resolved modules
        if (callback) {
            callback(...resolved);
        }
        
        return resolved[0];
    }
    
    return { define, require };
})();

console.log('AMD Module Definition:');

// Pattern 1: Named module with dependencies
AMD.define('constants', [], function() {
    return {
        PI: 3.14159,
        E: 2.71828
    };
});

// Pattern 2: Module depending on another module
AMD.define('geometry', ['constants'], function(constants) {
    return {
        circleArea(radius) {
            return constants.PI * radius * radius;
        },
        circleCircumference(radius) {
            return 2 * constants.PI * radius;
        }
    };
});

// Pattern 3: Simple module (no dependencies)
AMD.define('utils', function() {
    return {
        capitalize(str) {
            return str.charAt(0).toUpperCase() + str.slice(1);
        }
    };
});

// Pattern 4: Object literal
AMD.define('config', {
    apiUrl: 'https://api.example.com',
    timeout: 5000
});

// Using require to load and use modules
AMD.require(['geometry', 'constants'], function(geometry, constants) {
    console.log('  Circle area (r=5):', geometry.circleArea(5).toFixed(2));
    console.log('  PI constant:', constants.PI);
});
console.log('');


// ═══════════════════════════════════════════════════════════════════════════════
// 2.2 AMD SPECIAL DEPENDENCIES
// ═══════════════════════════════════════════════════════════════════════════════

/*
ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”
│                       AMD SPECIAL DEPENDENCIES                                   │
ā”œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”¤
│                                                                                  │
│  Special AMD dependencies:                                                      │
│                                                                                  │
│  • require: Local require function for dynamic loading                          │
│  • exports: Object to attach exports to                                         │
│  • module: The module object (like CommonJS)                                    │
│                                                                                  │
│  ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”   │
│  │  define(['require', 'exports', 'module'], function(require, exports, module) {│
│  │      // CommonJS-style inside AMD                                       │   │
│  │      const dep = require('./dep');                                      │   │
│  │      exports.value = 42;                                                │   │
│  │      module.exports = { ... };                                          │   │
│  │  });                                                                    │   │
│  ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜   │
│                                                                                  │
ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜
*/

console.log('AMD Special Dependencies:');

// Simplified CommonJS wrapper in AMD
AMD.define('commonjs-style', [], function() {
    // Simulating CommonJS-style module
    const exports = {};
    
    exports.greet = function(name) {
        return `Hello, ${name}!`;
    };
    
    exports.VERSION = '1.0.0';
    
    return exports;
});

AMD.require(['commonjs-style'], function(mod) {
    console.log('  greet("AMD"):', mod.greet('AMD'));
    console.log('  VERSION:', mod.VERSION);
});
console.log('');


// ════════════════════════════════════════════════════════════════════════════════
// PART 3: UMD (UNIVERSAL MODULE DEFINITION)
// ════════════════════════════════════════════════════════════════════════════════

/*
ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”
│                              UMD PATTERN                                         │
ā”œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”¤
│                                                                                  │
│  UMD = Universal Module Definition                                              │
│  Works in: CommonJS (Node.js), AMD (RequireJS), and Browser Globals             │
│                                                                                  │
│  ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”   │
│  │                                                                         │   │
│  │  (function(root, factory) {                                             │   │
│  │      if (typeof define === 'function' && define.amd) {                  │   │
│  │          // AMD                                                         │   │
│  │          define(['dependency'], factory);                               │   │
│  │      } else if (typeof module === 'object' && module.exports) {         │   │
│  │          // CommonJS                                                    │   │
│  │          module.exports = factory(require('dependency'));               │   │
│  │      } else {                                                           │   │
│  │          // Browser globals                                             │   │
│  │          root.MyModule = factory(root.Dependency);                      │   │
│  │      }                                                                  │   │
│  │  })(this, function(dependency) {                                        │   │
│  │      return { /* module contents */ };                                  │   │
│  │  });                                                                    │   │
│  │                                                                         │   │
│  ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜   │
│                                                                                  │
ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜
*/

// UMD Pattern Example
(function(root, factory) {
    // Check for AMD
    if (typeof AMD !== 'undefined' && AMD.define) {
        AMD.define('umdModule', [], factory);
    }
    // Check for CommonJS (Node.js)
    else if (typeof module === 'object' && module.exports) {
        module.exports = factory();
    }
    // Browser globals
    else {
        root.UMDModule = factory();
    }
})(typeof globalThis !== 'undefined' ? globalThis : this, function() {
    // Module factory function
    return {
        name: 'UMD Module',
        version: '1.0.0',
        
        greet(name) {
            return `Hello from UMD, ${name}!`;
        },
        
        calculate(a, b) {
            return {
                sum: a + b,
                product: a * b,
                difference: a - b
            };
        }
    };
});

console.log('UMD Pattern:');
// Access via AMD
AMD.require(['umdModule'], function(umd) {
    console.log('  UMD greet("World"):', umd.greet('World'));
    console.log('  UMD calculate(5, 3):', umd.calculate(5, 3));
});
console.log('');


// ════════════════════════════════════════════════════════════════════════════════
// PART 4: COMMONJS VS ES MODULES DIFFERENCES
// ════════════════════════════════════════════════════════════════════════════════

/*
ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”
│                     COMMONJS VS ES MODULES                                       │
ā”œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”¤
│                                                                                  │
│  ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”¬ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” │
│  │  Feature             │  CommonJS              │  ES Modules              │ │
│  ā”œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”¼ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”¼ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”¤ │
│  │  Syntax              │  require/exports       │  import/export           │ │
│  │  Loading             │  Synchronous           │  Asynchronous            │ │
│  │  Structure           │  Dynamic               │  Static                  │ │
│  │  When parsed         │  Runtime               │  Compile time            │ │
│  │  Binding             │  Copy of value         │  Live binding            │ │
│  │  Top-level await     │  No                    │  Yes (ES2022)            │ │
│  │  Tree-shaking        │  No                    │  Yes                     │ │
│  │  Strict mode         │  Optional              │  Always                  │ │
│  │  this                │  module object         │  undefined               │ │
│  │  File extension      │  .js, .cjs             │  .mjs, .js (type:module) │ │
│  ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”“ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”“ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ │
│                                                                                  │
ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜
*/

console.log('CommonJS vs ES Modules - Key Differences:');

// 1. Value Copy vs Live Binding
console.log('\n  1. Value Copy (CommonJS) vs Live Binding (ES Modules):');

// CommonJS: exports a copy
const cjsCounter = (function() {
    let count = 0;
    return {
        count: count,  // This is a copy!
        increment() { 
            count++; 
            return count;
        }
    };
})();

console.log('     CommonJS-style counter:', cjsCounter.count); // 0
cjsCounter.increment();
console.log('     After increment, exported count:', cjsCounter.count); // Still 0!
console.log('     Actual count via method:', cjsCounter.increment() - 1); // 1

// ES Modules would have live binding - the value updates automatically

// 2. Dynamic vs Static
console.log('\n  2. Dynamic (CommonJS) vs Static (ES Modules):');
console.log('     CommonJS: require() can be anywhere, conditional');
console.log('     ES Modules: import must be at top level');

// CommonJS allows:
/*
if (condition) {
    const module = require('./module');
}
*/

// ES Modules require import() for dynamic:
/*
if (condition) {
    const module = await import('./module');
}
*/

// 3. this binding
console.log('\n  3. this Binding:');
console.log('     CommonJS: this === exports (module object)');
console.log('     ES Modules: this === undefined');

console.log('');


// ════════════════════════════════════════════════════════════════════════════════
// PART 5: REAL-WORLD EXAMPLES
// ════════════════════════════════════════════════════════════════════════════════

console.log('Real-World Module Examples:');

// Example 1: Node.js style HTTP module (CommonJS)
const httpModule = CommonJSSimulator.require((module) => {
    const createServer = (handler) => {
        return {
            listen(port, callback) {
                console.log(`    Server listening on port ${port}`);
                if (callback) callback();
            }
        };
    };
    
    const get = (url, options) => {
        return Promise.resolve({ statusCode: 200, data: 'response' });
    };
    
    module.exports = { createServer, get };
});

console.log('  HTTP Module (CommonJS style):');
const server = httpModule.createServer((req, res) => {});
server.listen(3000);

// Example 2: Configuration Module
const configModule = CommonJSSimulator.require((module) => {
    const env = process.env?.NODE_ENV || 'development';
    
    const configs = {
        development: {
            apiUrl: 'http://localhost:3000',
            debug: true
        },
        production: {
            apiUrl: 'https://api.production.com',
            debug: false
        }
    };
    
    module.exports = configs[env] || configs.development;
});

console.log('  Config Module:', configModule);
console.log('');


// ════════════════════════════════════════════════════════════════════════════════
// SUMMARY
// ════════════════════════════════════════════════════════════════════════════════

console.log(`
╔══════════════════════════════════════════════════════════════════════════════╗
ā•‘                   COMMONJS & AMD - EXAMPLES COMPLETE                         ā•‘
╠══════════════════════════════════════════════════════════════════════════════╣
ā•‘                                                                              ā•‘
ā•‘  CommonJS (Node.js):                                                         ā•‘
ā•‘  • module.exports = {...} to export                                          ā•‘
ā•‘  • require('./module') to import                                             ā•‘
ā•‘  • Synchronous loading                                                       ā•‘
ā•‘  • Values are copied (not live)                                              ā•‘
ā•‘  • Still widely used in Node.js                                              ā•‘
ā•‘                                                                              ā•‘
ā•‘  AMD (RequireJS):                                                            ā•‘
ā•‘  • define(name, deps, factory) to define                                     ā•‘
ā•‘  • require(deps, callback) to load                                           ā•‘
ā•‘  • Asynchronous loading                                                      ā•‘
ā•‘  • Designed for browsers                                                     ā•‘
ā•‘  • Less common today (ES Modules preferred)                                  ā•‘
ā•‘                                                                              ā•‘
ā•‘  UMD (Universal):                                                            ā•‘
ā•‘  • Works in both environments                                                ā•‘
ā•‘  • Good for library distribution                                             ā•‘
ā•‘  • Complex wrapper pattern                                                   ā•‘
ā•‘                                                                              ā•‘
ā•‘  Modern Recommendation:                                                      ā•‘
ā•‘  • Use ES Modules when possible                                              ā•‘
ā•‘  • Node.js supports both (package.json "type": "module")                     ā•‘
ā•‘  • Bundlers handle interop                                                   ā•‘
ā•‘                                                                              ā•‘
ā•šā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•
`);
Examples - JavaScript Tutorial | DeepML