π‘ μΉν©μ
λͺ¨λ λ²λ€λ¬
μ΄λ€!
μΉν©μ μ€λͺ νκΈ° μν΄ μ΄ν΄νκ³ λμ΄κ°μΌν κ²μ΄ λ°λ‘ "λͺ¨λ" μ λλ€. π
A module is one of the separate parts.
// μ΄λ κ² κ°μΈμ νλμ λͺ¨λμ λ§λ€ μ μμ΅λλ€.
(function () {
// scopeκ° λ§νμκΈ° λλ¬Έμ μ μλ³μκ° μμ±λμ§ μμ΅λλ€.
// μ¦μ μ€νν¨μλ λ³΄ν΅ μΌνμ© μ½λ
})();
// λͺ¨λ λ νλ
(function () {
window.myLibrary = /* */;
}());
// λͺ¨λ λ νλ
(function () {
var math = {
add: function () {}
}
window.math = math;
}());
//kakao μ½λ μ΄λ°
(function (f) {
if ((typeof exports === "undefined" ? "undefined" : _typeof(exports)) === "object" && typeof module !== "undefined") {
module.exports = f();
} else if (typeof define === "function" && define.amd) {
define([], f);
} else {
var g;
if (typeof window !== "undefined") {
g = window;
} else if (typeof global !== "undefined") {
g = global;
} else if (typeof self !== "undefined") {
g = self;
} else {
g = this;
}
g.Kakao = f();
}
})(function() { return /* λ΄λΆ μ½λ */ })
ES Module
<!-- IEλ μμ§ μ§μ μ λ¨. μ¬μ©νλ €λ©΄ type μμ± μΆκ° -->
<script type="module" src="index.js" />
// index.js
import fn, { name } from './module';
console.log(name);
console.log(fn());
// module.js
export const name = 'λͺ¨λ';
export default function() {
console.log('μ΅λͺ
ν¨μ')
}
babel
μ ES6 β λΈλΌμ°μ μμ μ¬μ©ν μ μλλ‘ λͺ¨λμ commonjsλ‘ λ°κΏμ£Όκ³ ,webpack
μ λͺ¨λκ°μ μμ‘΄μ±μ νμ
νμ¬ μλ°μ€ν¬λ¦½νΈ μ½λλ₯Ό bundle(ν©μ³) ν΄μ€λ€.CommonJS
// routes.js
const routes = { name: "/main" };
module.exports = routes;
// index.js
const routes = require("./routes");
console.log(routes); // { name: '/main' }