GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing zetcode.com — JavaScript Modules - Organizing Code Effectively JavaScript modulelast modified last modified October 18, 2023 In this tutorial, we show how to create and use modules in JavaScript. A module is a container for JavaScript code. A module is defined in a separate file. The purpose of a module is to organize and isolate code. JavaScript did not have a module system for many years. As a result, several third-party solutions were created. The most popular was CommonJS. In 2015 a standard JS module system was released. In 2019, the new module system was ported to Node.js. Important attributes of modules:
<script type="module" src="main.js"></script>
We enable modules in the browser with the {
...
"main": "main.js",
"type": "module",
...
}
In Node.js, the ES module system is enabled with the
There is a convention to use the Export/import keywords
The export const name = 'John Doe'; -> import { name } from 'mymod.mjs';
This is the basic name export. export default name = 'John Doe'; -> import name from 'mymod.mjs'; This is the syntax for the default export. export { name as username }; -> import { username } from 'mymod.mjs';
A rename export syntax. export { import {
fname, fname,
lname as last_name; -> last_name
}; } from 'mymod.mjs';
This is the syntax for list export. JS module example in browserIn the following example, we create a simple module. index.html
<!DOCTYPE html>
<html>
<head>
<title>Drawing</title>
<script type="module" src="main.mjs"></script>
<style>
canvas {
border: 1px solid black;
}
</style>
</head>
<body>
<canvas id="board" width="550" height="550">
</canvas>
</body>
</html>
we draw on a canvas. The drawing code is located in the main.mjs
import { drawLine, drawCircle } from './modules/draw.mjs';
let canvas = document.getElementById('board');
let ctx = canvas.getContext('2d');
drawLine(ctx);
drawCircle(ctx);
In the modules/draw.mjs
export function drawLine(ctx) {
ctx.beginPath();
ctx.moveTo(20, 20);
ctx.lineTo(250, 250);
ctx.lineWidth = 3;
ctx.stroke();
}
export function drawCircle(ctx) {
ctx.beginPath();
ctx.arc(250, 90, 80, 0, 2*Math.PI);
ctx.fill();
}
We have the definitions of the two functions. They are prefixed with the
JS module example in Node.jsIn the next example, we set up a module in Node.js. {
...
"main": "main.js",
"type": "module",
...
"license": "ISC"
}
In the modules/core.mjs
const fname = 'John';
const lname = 'Doe';
const occupation = 'gardener';
function say_message() {
console.log(`${fname} ${lname} is a ${occupation}`);
}
export {
fname as first_name,
lname as last_name,
say_message
}
In the main.js
import { first_name, last_name, say_message } from "./modules/core.mjs";
console.log(first_name);
console.log(last_name);
say_message();
Inside the $ node main.js John Doe John Doe is a gardener SourceJavaScript Modules - language reference In this article we have worked with modules in JavaScript. AuthorMy name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming. |
| You are visitor 01402520 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |