module.exports 对象是由模块系统创建的。在我们自己写模块的时候,需要在模块最后写好模块接口,声明这个模块对外暴漏声明内容,module.exports提供了暴漏接口的方法。

1、返回一个JSON object

 var app = {
name:'app',
version:'1.0.0',
sayName:function(name) {
console.log(this.name);
}
} module.exports = app;

这种方法可以返回全局共享的变量或者方法。

调用方法:

1 var app = require("./app.js");
2 app.sayName('hello');//hello

或者这样用:

 var func1 = function(){
console.log("func1");
}; var func2 = function(){
console.log("fun2");
}; exports.function1 = func1;
exports.function2 = func2;

调用方法为:

var functions = require("./functions");
functions.function1();
functions.function2();

2、返回一个构造函数

CLASS.js

var CLASS = function(args) {
this.args = args;
} module.exports = CLASS;

调用:

var CLASS = require("./CLASS.js");
var c = new CLASS('arguments');

3、返回一个实例对象:

//CLASS.js
var CLASS = function() {
this.name = "class";
}
CLASS.prototype.func = function(){
alert(this.name);
}
module.exports = new CLASS();

调用:

var c = require("./CLASS.js");
c.func();//"class"

module.exports用法的更多相关文章

  1. export与export default exports与module.exports的用法

    转载:http://blog.csdn.net/zhou_xiao_cheng/article/details/52759632 本文原创地址链接:http://blog.csdn.net/zhou_ ...

  2. nodejs中exports与module.exports的实践

    只要是在nodejs中写自己的文件模块就少不了会遇到module.exports和exports的使用,看别人的代码大多都会使用“module.exports=exports=<对象/函数等&g ...

  3. node基础再现--module.exports 和exports

    实际上,最最基础的方法,最最原始的方法是module.exports,至于exports,是为了方便书写才出来的,应该说,module.exports 包含exports,所工作的范围更加的广泛! m ...

  4. 探讨ES6的import export default 和CommonJS的require module.exports

    今天来扒一扒在node和ES6中的module,主要是为了区分node和ES6中的不同意义,避免概念上的混淆,同时也分享一下,自己在这个坑里获得的心得. 在ES6之前 模块的概念是在ES6发布之前就出 ...

  5. module.exports 、exports、export、export default的区别

    module.exports和exports是属于 CommonJS 模块规范,export和export default是属于ES6语法. module.exports和exports导出模块,用r ...

  6. Node.js模块导出exports 和 module.exports 的区别

    原文: https://blog.csdn.net/Pwiling/article/details/51958693 每一个node.js执行文件,都自动创建一个module对象,同时,module对 ...

  7. 简单介绍export default,module.exports与import,require的区别联系

    他们都是成对使用的,不能乱用: module.exports 和 exports是属于CommonJS模块规范,对应---> require属于CommonJS模块规范 export 和 exp ...

  8. exports和module.exports的区别——学习笔记

    一开始,exports和module.exports都指向空对象(同一内存块),exports是引用 module.exports的值.module.exports 被改变的时候,exports不会被 ...

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

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

随机推荐

  1. 46. 47. Permutations

    求全排列. 1. 无重复元素 Given a collection of distinct numbers, return all possible permutations. For example ...

  2. 数据库到jsp页面报错(一)

    数据库到jsp页面报错(一) 这个错误的确比较坑. 控制台:     页面: 解决: 神坑啊!!!

  3. Activiti工作流笔记(4)

    Activiti工作流启动流程 /** * 启动流程 * */ public class ActivitiTest2 { RepositoryService repositoryService; Ru ...

  4. 根据Request获取真实客户端IP

    转载:http://www.cnblogs.com/icerainsoft/p/3584532.html 在JSP里,获取客户端的IP地址的方法是:request.getRemoteAddr() ,这 ...

  5. 提高C++程序运行效率的10个简单方法

    转载: 一.尽量减少值传递,多用引用来传递参数.至于其中的原因,相信大家也很清楚,如果参数是int等语言自定义的类型可能能性能的影响还不是很大,但是如果参数是一个类的对象,那么其效率问题就不言而喻了. ...

  6. O(logn)二叉树中的意义----高性能(四)

    转载地址:https://zhidao.baidu.com/question/239708227508660244.html?qbl=relate_question_2&word=%CA%B1 ...

  7. linux physical and virtual addressing modes

    example 1: 特理地址和虚拟地址一致 Physical addressing mode requires no page tables and the CPU does not attempt ...

  8. react中为什么要使用immutable

    因为在react中,react的生命周期中的setState()之后的shouldComponentUpdate()阶段默认返回true,所以会造成本组件和子组件的多余的render,重新生成virt ...

  9. php截取中文字符串 GB2312 utf-8

    UTF-8截取中文字符串 function Cn_Substr($string, $length) { preg_match_all("/[\x01-\x7f]|[\xc2-\xdf][\x ...

  10. SSH项目搭建(三)——Maven多模块搭建项目

    多模块开发,大致的思想就是把一个项目按某种方式分成多个模块,再把模块们连接成一个整体,我们在开发的时候,可以很清晰的操作每一个模块,可以大大提高开发的效率. Java web项目,最常见的就是按代码的 ...