console.log(this) 特性2:每个 ES module 都是运行在单独的私有作用域中 ..._浏览器esm特性">
当前位置:   article > 正文

ES Modules【浏览器端的模块开发规范】_浏览器esm特性

浏览器esm特性

目录

一、ES Modules 特性

二、ES Modules 导出

三、ES Modules 浏览器环境 Polyfill 

四、ES Modules in Node.js - 支持情况

五、 ES Modules in Node.js - 与 CommonJS 交互

六、ES Modules in Node.js - 与 CommonJS 的差异

七、ES Modules in Node.js - 新版本进一步支持 

八、 ES Modules in Node.js - Babel 兼容方案 


一、ES Modules 特性

  • 特性1:EMS 自动采用严格模式,忽略 ‘use strict’ 
  1. <script type="module">
  2. console.log(this)
  3. </script>
  • 特性2:每个 ES module 都是运行在单独的私有作用域中 
  1. <script type="module">
  2. var foo = 100
  3. console.log(foo) //100
  4. </script>
  5. <script type="module">
  6. console.log(foo) //找不到
  7. </script>
  • 特性3:ESM 是通过 CORS 的方式请求外部 JS 模块的 
<script type="module" src="https://unpkg.com/jquery@3.4.1/dist/jquery.min.js"></script> 
  •  特性4,:ESM 的 script 标签会延迟执行脚本

二、ES Modules 导出

  • import 是载入模块
     
    1. import { name, hello, Person } from './module.js'
    2. console.log(name, hello, Person)
    3. import { name } from './module.js' //不能省略 .js
    4. import { name } from './module.js' //不能省略 ./
    5. import { name } from '04-import//module.js'
    6. import { } from './module.js' //只是导出模块,并不会提取成员
    7. import './module.js'
    8. import * as mod from './module.js' //导出所有成员
    9. import('./module.js').then(function (modile) { //动态导入模块
    10. console.log(modile)
    11. })
    12. import { name, age, default as title } from './module.js' //导出默认成员
    13. import title, { name, age } from './module.js'
  • export 是导出模块 
    1. //index.js
    2. export { default as button } from './button.js'
    3. export { Avatar } from './avatar.js'
    4. //button.js
    5. export var BUtton = 'Button Component'
    6. export default Button
    7. //avatar.js
    8. export var Avatar = '
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/862812
推荐阅读
  

闽ICP备14008679号