J

ES6 Module

JavaScript Syntax Guide

Importieren und Exportieren von Modulen in JavaScript

ES6 Module

Importieren und Exportieren von Modulen in JavaScript

JavaScript es6 module (javascript)
        
          // Exporting (in math.js)
// Named exports
export const PI = 3.14159;
export function add(a, b) {
  return a + b;
}

// Default export
export default class Calculator {
  multiply(a, b) {
    return a * b;
  }
}

// Importing
import { PI, add } from './math.js';
import Calculator from './math.js';

// Or import everything
import * as MathUtils from './math.js';

// Usage
console.log(PI); // 3.14159
console.log(add(2, 3)); // 5

const calc = new Calculator();
console.log(calc.multiply(4, 5)); // 20

// Dynamic imports
const module = await import('./math.js');
console.log(module.PI);
        
      

Explanation

ES6-Module bieten eine Standardmethode zur Organisation und gemeinsamen Nutzung von Code. Verwende benannte Exporte für mehrere Exporte und Standardexport für den Hauptexport.

Common Use Cases

  • Code-Organisation
  • Wiederverwendbarkeit
  • Abhängigkeitsverwaltung
  • Tree Shaking

Related JavaScript Syntax

Master ES6 Module in JavaScript

Understanding ES6 Module is fundamental to writing clean and efficient JavaScript code. This comprehensive guide provides you with practical examples and detailed explanations to help you master this important concept.

Whether you're a beginner learning the basics or an experienced developer looking to refresh your knowledge, our examples cover real-world scenarios and best practices for using ES6 Module effectively in your JavaScript projects.

Key Takeaways

  • Code-Organisation
  • Wiederverwendbarkeit
  • Abhängigkeitsverwaltung