最近在看《node开发实战详解》时有写疑问,所以自己就整理了一些资料。下面是node4.*的官方api文档(http://nodejs.cn/doc/node_4/modules.html#modules_module_exports),我有点看不懂,就拉出node.10*的官方api(https://nodejs.org/dist/v0.10.9/docs/api/modules.html#modules_module_exports)。

module.exports与exports的介绍

module.exports与exports都是将函数或者是方法暴露出去,require的时候进行调用,但是2者是有区别的。以下是代码:

//ex.js

exports='danhuangmode';

//mex.js

module.exports='danhuangmode';

//call_ex_mex.js

var ex=require('./ex');
  var mex=require('./mex');

console.log(ex);
  console.log('\n');
  console.log(mex);

执行结果:

引用exports提供方法,输出是为一个对象,引用module.exports提供方法,输出为字符串。

exports内部提供接口,在外部引用时之间的关系如何?

exports内部处理暴露方法,是如何处理的,看如下代码:

var string='this is in exports.js';

 function ex_fn () {
console.log('this in funtion ex_fn');
} var exobj={
str1:"str1 exobj",
exobjfn: function () {
console.log("in function");
}
}; exports.string=string;
exports.ex_fn=ex_fn;
exports.exobj=exobj;
exports=exobj;

调用代码:

var ex=require('./ex');

console.log(ex.string);
console.log(ex.ex_fn);
console.log(ex.exobj);
console.log(ex);

结果显示:

exports提供的所有接口,直接调用导出实例化的接口对象,会显示接口对象中所有提供的类型、函数、对象以及对象中的方法和对象。

module.exports对外提供接口如何处理?

//mex.js

var ex_fn= function () {
console.log('this in funtion ex_fn');
} module.exports=ex_fn;

调用代码mex.js:

//引用mex.js

var ex=require('./mex');
ex();
console.log(ex);

 

执行结果为:

直接将函数作为返回。

再看下面一个例子:

var person={
name :"person's name",
age :20,
getAge: function () {
return this.age;
}
} module.exports = person;

调用的代码:

var person=require('./modulex');

console.log(person.name);
console.log(person.age);
console.log(person.getAge());
console.log(person);

显示的结果为:

返回为一个json对象,可以直接调用内部的函数、属性。

module.exports 与exports是什么关系?

module.exports = 'personname';

exports.name=function  () {
console.log('this is person name');
}

调用 的脚本:

var person=require('./person');

console.log(person);
console.log(person.name);

执行结果:

personname
undefined

结果:

其实真正的接口是module.exports,exports是一个辅助工具。最终返回到是module.exports,而不是exports。

当module.exports没有任何属性和方法,exports将收集的所有信息都传递给module.exports,如果module.exports已经具有了属性和方法,exports所搜集的信息将会被忽略。

Node中Exports与module.export的使用与区别的更多相关文章

  1. node中exports和module.exports的关系及使用

    在node中,需要记住,在使用exports和module.exports的时候,实际输出的是module.exports. exports指向module.exports,是module.expor ...

  2. Node.js exports与module.exports的关系

    今天搜索module.exports时看到CNode社区上发的Hack Sparrow一篇相关文章的链接 Node.js Module – exports vs module.exports 一篇5年 ...

  3. Node.js中exports,module.exports以及require方法

    在Node.js中,使用module.exports.f = ...与使用exports.f = ...是一样的,此时exports就是module.exports的一种简写方式.但是,需要注意的是, ...

  4. Node.js中exports与module.exports的区别

    原文:http://www.hacksparrow.com/node-js-exports-vs-module-exports.html 你肯定对Node.js模块中用来创建函数的exports对象很 ...

  5. node.js模块中exports和module.exports的区别

    Node应用由模块组成,采用CommonJS模块规范. 根据这个规范,每个文件就是一个模块,有自己的作用域.在一个文件里面定义的变量.函数.类,都是私有的,对其他文件不可见. CommonJS规范规定 ...

  6. Node.js 中 exports 和 module.exports 的区别

    每一个模块中都有一个 module 对象, module 对象中有一个 exports 对象 我们可以把需要导出的成员都放到 module.exports 这个接口对象中,也就是 module.exp ...

  7. nodejs模块中exports和module.exports的区别

    通过Node.js的官方API可以看到Node.js本身提供了很多核心模块 http://nodejs.org/api/ ,这些核心模块被编译成二进制文件,可以require('模块名')去获取:核心 ...

  8. nodejs中exports与module.exports的区别详细介绍

    如果模块是一个特定的类型就用Module.exports.如果模块是一个典型的"实例化对象"就用exports. exports.name = function() { conso ...

  9. vue中的import、export、requre的区别

    在es6之前js一直没有自己的模块语法,为了解决这种尴尬就有了require.js的出现.在es6发布之后js又引入了import的概念使得不清楚两者之间的区别的同学在实际使用过程中造成了自己的误解, ...

随机推荐

  1. salt-API基本验证命令

    配置SALT-API,网上有很多,作下来也很顺利. 我的参考: 作一下验证的记录: curl -k https://x.x.x.x:8000/login -H "Accept: applic ...

  2. Spring MVC 解读——@RequestMapping (2)(转)

    转自:http://my.oschina.net/HeliosFly/blog/214438 Spring MVC 解读——@RequestMapping 上一篇文章中我们了解了Spring如何处理@ ...

  3. BZOJ3715: [PA2014]Lustra

    3715: [PA2014]Lustra Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 237  Solved: 149[Submit][Status ...

  4. BZOJ2342: [Shoi2011]双倍回文

    2342: [Shoi2011]双倍回文 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 923  Solved: 317[Submit][Status ...

  5. BZOJ1614: [Usaco2007 Jan]Telephone Lines架设电话线

    1614: [Usaco2007 Jan]Telephone Lines架设电话线 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 892  Solved: ...

  6. Unity3D学习笔记——选择Enemy

    一.步骤: 1.创建三个Cube,并将这三个Cube的Cube的Tag设为Enemy 2.导入第一人称视角的资源 3.创建名为Targeting的C#脚本 4.编写Targeting脚本,并将它附到第 ...

  7. JavaScript高级程序设计14.pdf

    继承,ECMAScript只支持实现继承,而且其实现继承主要是依靠原型链来实现的 构造函数.原型.和实例的关系:每个构造函数都有一个原型对象,每个原型对象都包含一个指向构造函数的指针,每个实例都包含一 ...

  8. Hdu 2475-Box LCT,动态树

    Box Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submis ...

  9. AC自动机——多模式串匹配的算法思想

    标准KMP算法用于单一模式串的匹配,即在母串中寻求一个模式串的匹配,但是现在又存在这样的一个问题,如果同时给出多个模式串,要求找到这一系列模式串在母串存在的匹配个数,我们应该如何处理呢? 基于KMP算 ...

  10. php中传值与传引用的区别。什么时候传值什么时候传引用?

    值传递:   函数范围内对值的任何改变在函数外部都会被忽略; 引用传递: 函数范围内对值的任何改变在函数外部也能反映出这些修改: 优缺点:按值传递时,php必须复制值.特别是对于大型的字符串和对象来说 ...