首先,咱們要明白一個前提,那就是CommonJS模塊規範與ES模塊規範是不一樣的概念函數
// example.js
let x = 5;
let addX = (value) => {
return x + value;
}
module.exports.x = x;
module.exports.addX = addX;
複製代碼
let requireTest = require(./example.js);
let x = requireTest.x;
let addX = requireTest.addX(3);
console.log(x); // 5
console.log(addx); // 8
複製代碼
let exports = module.exports;
複製代碼
let firstName = "Chengwu";
let lastName = "Du";
export { firstName, lastName }
複製代碼
// one
export const PI = "3.1415926";
// two
let name = "Robin";
export { name }
// three
let n = "Robin";
export { n as name }
複製代碼
import { firstName, lastName } from "./export.js";
let name = firstName + lastName;
console.log(name); // Chengwu Du
複製代碼
export defalut function() {
return "Robin";
}
複製代碼