# ESM 和 CommonJS 的区别
不同点:
this指向不同CommonJS的this指向的是当前module, 默认是exportsvar args = [self.exports, require, self, filename, dirname] return compiledWrapper.apply(self.exports, args)1
2ESM由于语言层面的设计指向的是undefined__filename,__dirname在CommonJS中存在, 而ESM中不存在在 CommonJS 中, 模块的执行需要用函数包裹, 并制定一些常用值. 所以我们在全局中可以直接用
__filename,__dirname.NativeModule.wrapper = [ '(function (exports, require, module, __filename, __dirname) {', '\n});' ]1
2
3
4
相同点:
ESM和CommonJS都有缓存加载一次后将其缓存起来, 后续再次加载就直接用缓存中的