最近在看《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. 【UVA11478】Halum (最短路解差分约束)

    题目: Sample Input2 11 2 102 11 2 -103 31 2 42 3 23 1 54 52 3 44 2 53 4 23 1 01 2 -1Sample OutputInfin ...

  2. mysql主从配置(清晰的思路)

    mysql主从配置.鄙人是在如下环境测试的: 主数据库所在的操作系统:win7 主数据库的版本:5.0 主数据库的ip地址:192.168.1.111 从数据库所在的操作系统:linux 从数据的版本 ...

  3. struts2+jsp+jquery+Jcrop实现图片裁剪并上传

    <1> 使用html标签上传需要裁剪的大图. <2> 在页面呈现大图,使用Jcrop(Jquery)对大图进行裁剪,并且可以进行预览. <3> 选择好截取部分之后发 ...

  4. 14.6.4 Configuring the Memory Allocator for InnoDB 配置InnoDB 内存分配器

    14.6.4 Configuring the Memory Allocator for InnoDB 配置InnoDB 内存分配器 当InnoDB 被开发时,内存分配提供了操作系统和 run-time ...

  5. [LeetCode#241]Different Ways to Add Parentheses

    Problem: Given a string of numbers and operators, return all possible results from computing all the ...

  6. C - Point on Spira

      Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Submit Status Pr ...

  7. nginx -- handler模块(100%)

    handler模块简介 相信大家在看了前一章的模块概述以后,都对nginx的模块有了一个基本的认识.基本上作为第三方开发者最可能开发的就是三种类型的模块,即handler,filter和load-ba ...

  8. position: absolute 的元素自动对齐父元素 border 外边缘

    Position with border outer edge CSS box-flex align-items justify-content

  9. 【转】iOS开发Xcode7真机调试教程

    原文网址:https://www.skyfox.org/ios-xcode7-debug-device.html 从Xcode7开始,Xcode 不需要$99/$299升级开发者直接可以进行真机调试 ...

  10. UVA 11762 Race to 1(记忆化+期望)

    题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=20869 [思路] DP+期望. 设f[x]表示从x转移到1的期望操 ...