使用命名导出
// module "my-module.js"
function cube(x) {
return x * x * x;
}
const foo = Math.PI + Math.SQRT2;
export { cube,foo };
在其它脚本 (比如import
)
import { cube, foo } from 'my-module.js';
console.log(cube(3)); // 27
console.log(foo); // 4.555806215962888
使用默认导出
// module "my-module.js"
export default function cube(x) {
return x * x * x;
}
在另一个脚本中,可以直接导入默认导出
// module "my-module.js"
import cube from 'my-module';
console.log(cube(3)); // 27