nodejs中module.exports和exports比较
2015-08-13 22:21
701 查看
首先定义一个module为math.js,在test.js中引用此module.
一下为通过几个使用情景来阐述其中的不同:
1.正常情况下,定义和使用module中的实例.
math.js:
test.js:
控制台中信息:
module:
math:
此时math是一个对象.
2.
math.js:
test.js:
控制台中信息:
module:
math:
如果使用
即便module中出现了
3.
math.js:
此时执行tes.js的情况和(2)中相同.如果module中出现多个module.exports,那最后一个会将前面的全部覆盖掉.所以math依旧是:
总结:可以将module.exports和exports看作两个普通的变量,在初始没有赋值的情况下它们指向的是同一对象.在使用require()时,只会传递module.exports中的对象.所以即便两个变量都赋值,也不会使用exports所指的对象.
参考:(https://cnodejs.org/topic/54e67b70629934692ea99f91)
一下为通过几个使用情景来阐述其中的不同:
1.正常情况下,定义和使用module中的实例.
math.js:
exports.add=function(arg1,arg2){ return arg1+arg2; }; exports.minus=function(arg1,arg2){ return arg1-arg2; }; console.log(module);
test.js:
var math=require('./math.js'); console.log('math:'+math); console.log(math.add(3,4)); console.log(math.minus(5,3));
控制台中信息:
module:
{ id: '/home/chander-zhang/NodeJS/test/math.js', exports: { add: [Function], minus: [Function] }, parent: { ... }, filename: '/home/chander-zhang/NodeJS/test/math.js', loaded: false, children: [], paths: [ '/home/chander-zhang/NodeJS/test/node_modules', '/home/chander-zhang/NodeJS/node_modules', '/home/chander-zhang/node_modules', '/home/node_modules', '/node_modules' ] }
math:
[object Object]
此时math是一个对象.
2.
math.js:
exports.add=function(arg1,arg2){ return arg1+arg2; }; minus=function(arg1,arg2){ return arg1-arg2; }; module.exports=minus; console.log(module);
test.js:
var math=require('./math.js'); console.log('math:'+math); //console.log(math.add(3,4)); console.log(math(5,3)); //相当于执行了 minus(5,3).
控制台中信息:
module:
{ id: '/home/chander-zhang/NodeJS/test/math.js', exports: [Function], parent: { ... }, filename: '/home/chander-zhang/NodeJS/test/math.js', loaded: false, children: [], paths: [ '/home/chander-zhang/NodeJS/test/node_modules', '/home/chander-zhang/NodeJS/node_modules', '/home/chander-zhang/node_modules', '/home/node_modules', '/node_modules' ] }
math:
function (arg1,arg2){ return arg1-arg2; }
如果使用
math.add()或
math.minus()将会出错,因为此时math是一个函数.
即便module中出现了
exports.add,由于为
module.exports赋了值,也不能使用
math.add().
3.
math.js:
add=function(arg1,arg2){ return arg1+arg2; }; minus=function(arg1,arg2){ return arg1-arg2; }; module.exports=add; module.exports=minus; console.log(module);
此时执行tes.js的情况和(2)中相同.如果module中出现多个module.exports,那最后一个会将前面的全部覆盖掉.所以math依旧是:
function (arg1,arg2){ return arg1-arg2; }
总结:可以将module.exports和exports看作两个普通的变量,在初始没有赋值的情况下它们指向的是同一对象.在使用require()时,只会传递module.exports中的对象.所以即便两个变量都赋值,也不会使用exports所指的对象.
参考:(https://cnodejs.org/topic/54e67b70629934692ea99f91)
相关文章推荐
- nodejs中exports与module.exports的区别
- Node.js 模块 包
- exports 和 module.exports 的区别
- nodejs之模块
- CommonJS简介及模块标准
- VS 2012 No exports were found that match the constraint 解决办法
- 文件共享服务之NFS的配置与应用
- NFS简单操作
- linux中的nfs服务
- nodejs中export与module.exports
- RedHat 7配置NFS服务
- [nodejs]exports和module.exports
- 【Node.js】module.exports和exports的区别与使用
- nodeJS实战:自定义模块与引入,不同模块的函数传递及回调处理,exports与module.exports(基于nodejs6.2.0)
- module.exports与exports的关系和区别
- node学习(2) -- 自定义模块(理解exports 和 module.exports的关系)
- exports构建自定义模块(一)
- webpack 前端加载工具 让 浏览器端 javascript 执行 CommonJS规范
- nodejs中 exports 和 module.exports 的区别
- NodeJs module 的初步理解