Module的加载实现

时间:2023-01-06 17:04:58

烂笔头开始记录小知识点啦~

  • 浏览器要加载 ES6模块,:
    <script type="module" src="./foo.js"></script>
  • 异步加载,相当与defer属性。可以另外设置async属性。
  • ES6 模块也允许内嵌在网页中,语法行为与加载外部脚本完全一致。
    <script type="module">
    import utils from "./utils.js"; // other code
    </script>
  • CommonJS 模块输出的是一个值的拷贝,ES6 模块输出的是值的引用。
  • CommonJS 模块是运行时加载,ES6 模块是编译时输出接口。