资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

node中module.exports与exports有什么区别

本篇内容主要讲解“node 中 module.exports 与 exports 有什么区别”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“node 中 module.exports 与 exports 有什么区别”吧!

成都创新互联公司-专业网站定制、快速模板网站建设、高性价比茌平网站开发、企业建站全套包干低至880元,成熟完善的模板库,直接使用。一站式茌平网站制作公司更省心,省钱,快速模板网站建设找我们,业务覆盖茌平地区。费用合理售后完善,十年实体公司更值得信赖。


exportsmodule.exports 的引用,类似如下所示

const exports = module.exports
 

那如下结果会如何导出?

module.exports = 100
exports = 3
 

很显然会导出 100,毕竟 exports 进行了重指向。

「那在 node 源码中如何实现的呢?」从源码里可以看出 「exports」的实质

node 中 module.exports 与 exports 有什么区别  
module wrapper

详见源码: https://github.com/nodejs/node/blob/master/lib/internal/modules/cjs/loader.js#L1252,可以看出符合猜想

众所周知,node 中所有的模块代码都被包裹在这个函数中

(function(exports, require, module, __filename, __dirname) {
  exports.a = 3
});
 

而以下源码指出,exports 是如何得来

const dirname = path.dirname(filename);
const require = makeRequireFunction(this, redirects);
let result;
// 从这里可以看出来 exports 的实质
const exports = this.exports;
const thisValue = exports;
const module = this;
if (requireDepth === 0) statCache = new Map();
if (inspectorWrapper) {
  result = inspectorWrapper(compiledWrapper, thisValue, exports,
                            require, module, filename, dirname);
} else {

  // 这里是模块包装函数
  result = compiledWrapper.call(thisValue, exports, require, module,
                                filename, dirname);
}

到此,相信大家对“node 中 module.exports 与 exports 有什么区别”有了更深的了解,不妨来实际操作一番吧!这里是创新互联网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!


网站题目:node中module.exports与exports有什么区别
链接分享:http://cdkjz.cn/article/gggooh.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220