ECMA Script Module(ES module)知识点

 1、每个 ES Module 都是运行在单独的私有作用,ESM 自动采用严格模式,忽略use strict

<script type="module">console.log(this);// this undefined 私有域,不能访问全局变量 </script>

<script type="module"> var foo = 100; console.log(foo)</script>

<script type="module"> console.log(foo);// foo undefined </script>

 

2、导出 export

export var name = "one export";  //单独导出一个

var name = "foo module";
function hello() {}


export { name, hello }  //加大括号,批量多个导出
export { name as default }  //默认导出,import时可取任意变量名
export {  hello as fooHello }  //改名导出,import时,取新的变量名

 

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

 

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

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

 

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

posted @ 2023-09-16 23:24  风别鹤  阅读(28)  评论(0编辑  收藏  举报