Comments (1)
ES6 模块与commonJS模块的差异
- commonJS 模块输出的是一个值的拷贝,ES6静态分析,动态引用,模块输出的是值的引用
commonJS模块一旦输出一个值,模块内部的变化就影响不到这个值。
ES6模块如果使用import从一个模块加载变量,那些变量不会被缓存,而是成为一个指向被加载模块的引用,原始值变了,import加载的值也会跟着变。需要开发者自己保证,真正取值的时候能够取到值。
- commonJS 模块是运行时加载,ES6 模块是编辑时输出接口
运行时加载:commonJS 模块就是对象;即在输入时是先加载整个模块,生成一个对象,然后再从这个对象上读取方法,这种加载称为“运行时加载”。commonJS脚本代码在require的时候,就会全部执行。一旦出现某个模板被“循环加载”,就只能输出已经执行的部分,还未执行的部分不会输出。
编译时加载:ES6 模块不是对象,而是通过export命令显式指定输出的代码,import时指定加载某个输出值,而不是加载整个模块,这种加载称为“编译时加载”。
-
commonJS 加载的是整个模块,即将所有的接口全部加载进来,ES6 可以单独加载其中的某个接口(方法)
-
commonJS this 指向当前模块,ES6 this 指向undefined
from fe-interview.
Related Issues (20)
- nuxt有哪些特性?为什么要用nuxt做服务端渲染?它解决了什么问题? HOT 1
- CSS Sprites(雪碧图)原理及其优缺点 HOT 1
- 图片懒加载原理及如何实现 HOT 1
- JS获取url参数的方法 HOT 1
- 手写实现一个合乎规范的Promise HOT 1
- requestAnimationFrame原理及兼容性封装 HOT 1
- 用过HappyPack吗?HappyPack有什么优点? HOT 2
- eslint忽略全局变量的方法 HOT 1
- webpack配置路径别名 HOT 1
- webpack添加路径别名后,vscode不能智能提示,如何解决? HOT 1
- 如何只用两行代码实现判断js中所有数据类型 HOT 1
- nrm和nvm HOT 1
- 不用window.open打开新窗口 HOT 1
- js变量提升 HOT 1
- js中精度问题及解决方案 HOT 2
- webpack之less-loader、css-loader、style-loader执行顺序 HOT 1
- js实现普通数组去重&json数组去重 HOT 1
- 常见的js模板引擎
- js原生实现拷贝到剪贴板 HOT 1
- iframe内嵌网页未知高度如何自适应 HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from fe-interview.