defaultエクスポートを使ってみる
functions.js
const add = function (x, y) {
return x + y;
};
export default add;index.js
import otherName from "./functions.js";
let ans = otherName(5, 7);
console.log(ans); // 12ここでは otherName という別名をつけています。
この時、{} は不要です。
名前付きエクスポートを使ってみる
functions.js
const display = function (name) {
console.log("Hello, ", name);
};
export const display2 = function (name) {
console.log("Hello2, ", name);
};
export { display };index.js
import { display, display2 } from "./functions.js";
display('ABC') // Hello, ABC
display2('XYZ') // Hello2, XYZfunctions.js で定義した名前で import する必要があります。