ECMA Script Module(ES module)知识点

发布时间 2023-09-16 23:26:11作者: 风别鹤

1、给 script 加 type = module,就可以以 ES Module 的标准执行 JS 代码
<script type="module"></script>

 

2、ESM 自动采用严格模式,忽略use strict
<script type="module">console.log(this)</script>

 

3,每个 ES Module 都是运行在单独的私有作用
<script type="module"> var foo = 100console.log(foo)</script>
<script type="module"> console.log(foo);// foo undefined </script>

 

4,ESM 是通过 CORS 的方式请求外部 JS 。

CORS,全称Cross-Origin Resource Sharing,是一种允许当前域(domain)的资源(比如html/js/web service)被其他域(domain)的脚本请求访问的机制,通常由于同域安全策略(the same-origin security policy)浏览器会禁止这种跨域请求。

 

5,延迟执行:网页渲染完成后,ESM 的 script 才会执行