| [ Web Proxy ] |
| Viewing: https://developer.mozilla.org/ko/docs/Web/JavaScript/Guide/Modules | [Back] [Original] |
Get to know MDN better
This page was translated from English by the community. Learn more and join the MDN Web Docs community.
JavaScript .
JavaScript . , . JavaScript , ( Node.js) JavaScript .
JavaScript , . node.js , JavaScript . ( RequireJS CommonJS AMD , Webpack Babel .)
, . , .
Github . <canvas> (element) , , .
, .
: , .
(basic-modules) .
index.html
main.js
modules/
canvas.js
square.js
: . .
modules .
canvas.js .
create() ID <div> , width height . ID( ) ( ) . 2D (wrapper div) ID .createReportList() , (div) (ul) . ID .square.js .
name 'square' .draw() , , . , , .reportArea() .reportPerimeter() . export . export (statement) .
(export) . .
export const name = "square";
export function draw(ctx, length, x, y, color) {
ctx.fillStyle = color;
ctx.fillRect(x, y, length, length);
return {
length: length,
x: x,
y: y,
color: color,
};
}
functions, var, let, const, class , . , export .
export . .
export { name, draw, reportArea, reportPerimeter };
, . .
import { name, draw, reportArea, reportPerimeter } from "./modules/square.js";
import (statement) , . from . ( , basic-modules /js-examples/modules/basic-modules ) main.js .
. " " (.) , . , URL .
,
/js-examples/modules/basic-modules/modules/square.js
.
./modules/square.js
main.js .
: . (:
'/modules/square'). JavaScript . .
. main.js import .
let myCanvas = create("myCanvas", document.body, 480, 320);
let reportList = createReportList(myCanvas.id);
let square1 = draw(myCanvas.ctx, 50, 50, 100, "blue");
reportArea(square1.length, reportList);
reportPerimeter(square1.length, reportList);
main.js HTML . HTML .
<script> (element) type="module" .
<script type="module" src="main.js"></script>
. Firefox , "SyntaxError: import declarations may only appear at top level of a module" .
import export (statement) . .
:
type="module"import . :<script type="module"> /* */ </script>.
file:// URL) , JavaScript CORS . .defer (<script> attributes) . defer. export named exports . (function, const ) export , import .
default export export . . JavaScript CommonJS AMD (interpolate) . (Jason Orendorff ES6 In Depth: Modules . "Default exports" )
. basic-modules square.js , , randomSquare() . export , .
export default randomSquare;
.
export default , .
export default function(ctx) {
...
}
main.js , default function import .
import randomSquare from "./modules/square.js";
, . default export randomSquare . .
import { default as randomSquare } from "./modules/square.js";
: export Renaming imports and exports .
. ? (shape) draw(), reportArea() . , .
. .
import export (statement) as , .
, .
// inside module.js
export { function1 as newFunctionName, function2 as anotherNewFunctionName };
// inside main.js
import { newFunctionName, anotherNewFunctionName } from "./modules/module.js";
// inside module.js
export { function1, function2 };
// inside main.js
import {
function1 as newFunctionName,
function2 as anotherNewFunctionName,
} from "./modules/module.js";
. renaming circle.js triangle.js , .
. export (statement) .
export { name, draw, reportArea, reportPerimeter };
main.js .
import { name, draw, reportArea, reportPerimeter } from "./modules/square.js";
import { name, draw, reportArea, reportPerimeter } from "./modules/circle.js";
import { name, draw, reportArea, reportPerimeter } from "./modules/triangle.js";
"SyntaxError: redeclaration of import name" . (Firefox).
import ( ) .
import {
name as squareName,
draw as drawSquare,
reportArea as reportSquareArea,
reportPerimeter as reportSquarePerimeter,
} from "./modules/square.js";
import {
name as circleName,
draw as drawCircle,
reportArea as reportCircleArea,
reportPerimeter as reportCirclePerimeter,
} from "./modules/circle.js";
import {
name as triangleName,
draw as drawTriangle,
reportArea as reportTriangleArea,
reportPerimeter as reportTrianglePerimeter,
} from "./modules/triangle.js";
import .
// in square.js
export {
name as squareName,
draw as drawSquare,
reportArea as reportSquareArea,
reportPerimeter as reportSquarePerimeter,
};
// in main.js
import {
squareName,
drawSquare,
reportSquareArea,
reportSquarePerimeter,
} from "./modules/square.js";
. , import . import .
, . . .
import * as Module from "./modules/module.js";
module.js export . Module .
Module.function1()
Module.function2()
etc.
. module-objects , . export .
export { name, draw, reportArea, reportPerimeter };
import .
import * as Canvas from "./modules/canvas.js";
import * as Square from "./modules/square.js";
import * as Circle from "./modules/circle.js";
import * as Triangle from "./modules/triangle.js";
, import . .
let square1 = Square.draw(myCanvas.ctx, 50, 50, 100, "blue");
Square.reportArea(square1.length, reportList);
Square.reportPerimeter(square1.length, reportList);
. ( ) import .
class export import . , .
class Square {
constructor(ctx, listId, length, x, y, color) {
...
}
draw() {
...
}
...
}
export .
export { Square };
main.js import .
import { Square } from "./modules/square.js";
.
let square1 = new Square(myCanvas.ctx, myCanvas.listId, 50, 50, 100, "blue");
square1.draw();
square1.reportArea();
square1.reportPerimeter();
. . export .
export * from "x.js";
export { name } from "x.js";
: import , export . , " x.js , export " .
module-aggregation . circle.js, square.js, triangle.js shapes.js . shapes . .
modules/
canvas.js
shapes.js
shapes/
circle.js
square.js
triangle.js
export . )
export { Square };
(aggregation) . shapes.js .
export { Square } from "./shapes/square.js";
export { Triangle } from "./shapes/triangle.js";
export { Circle } from "./shapes/circle.js";
export shapes.js .
:
shapes.jsexport , .
main.js .
import { Square } from "./modules/square.js";
import { Circle } from "./modules/circle.js";
import { Triangle } from "./modules/triangle.js";
.
import {
Square,
Circle,
Triangle,
} from "/js-examples/modules/module-aggregation/modules/shapes.js";
JavaScript . , . . .
import() (parameter) . (Creating a module object ) promise export .
import("/modules/myModule.js").then((module) => {
// Do something with the module.
});
. In the dynamic-module-imports classes . . ("Circle", "Square", "Triangle") . , .
index.html main.js . export .
main.js document.querySelector() . .
let squareBtn = document.querySelector(".square");
, .
squareBtn.addEventListener("click", () => {
import("/js-examples/modules/dynamic-module-imports/modules/square.js").then(
(Module) => {
let square1 = new Module.Square(
myCanvas.ctx,
myCanvas.listId,
50,
50,
100,
"blue",
);
square1.draw();
square1.reportArea();
square1.reportPerimeter();
},
);
});
Note that, promise fulfillment . Module (contructor) . Module.Square( ... ) Module .
This page was last modified on 2025 6 27 by MDN contributors.
Your blueprint for a better internet.
Portions of this content are 19982026 by individual mozilla.org contributors. Content available under a Creative Commons license.
| Web Proxy Viewer | New URL | Original Page |