Node.js之exports与module.exports

每个node.js执行文件,都自动建立一个module对象,同时,module对象会建立一个叫exports的属性,初始化的值是 {}javascript

 module.exports = {};
Node.js为了方便地导出功能函数,node.js会自动地实现如下这个语句

tool.jsjava

 exports.a = function(){
 console.log('a')
 }

 exports.a = 1
test.js
 var x = require('./tool');

 console.log(x.a)
 

看到这里,相信你们都看到答案了,exports是引用 module.exports的值。module.exports 被改变的时候,exports不会被改变,而模块导出的时候,真正导出的执行是module.exports,而不是exportsnode

再看看下面例子express

hammer.jsapp

 exports.a = function(){
  console.log('a')
 }

 module.exports = {a: 2}
 exports.a = 1
 

test.jsmongoose

 var x = require('./hammer');

 console.log(x.a)
 

result:ide

 2

exports在module.exports 被改变后,失效。函数

是否是开始有点廓然开朗,下面将会列出开源模块中,常常看到的几个使用方式。ui

##module.exports = Viewthis

function View(name, options) { 
   options = options || {};
   this.name = name;
   this.root = options.root;
   var engines = options.engines;
   this.defaultEngine = options.defaultEngine;
   var ext = this.ext = extname(name);
   if (!ext && !this.defaultEngine) throw new Error('No default engine was specified and no         extension was provided.');
   if (!ext) name += (ext = this.ext = ('.' != this.defaultEngine[0] ? '.' : '') +     this.defaultEngine);
   this.engine = engines[ext] || (engines[ext] = require(ext.slice(1)).__express);
   this.path = this.lookup(name);
 }

 module.exports = View;
 

javascript里面有一句话,函数即对象,View 是对象,module.export =View, 即至关于导出整个view对象。外面模块调用它的时候,可以调用View的全部方法。不过须要注意,只有是View的静态方法的时候,才可以被调用,prototype建立的方法,则属于View的私有方法。

tool.js

function View(){

 }

 View.prototype.test = function(){
  console.log('test')
 }

 View.test1 = function(){
  console.log('test1')
 }

 

module.exports = View

test.js

 var x = require('./tool');

 console.log(x) //{ [Function: View] test1: [Function] }
 console.log(x.test) //undefined
 console.log(x.test1) //[Function]
 x.test1() //test1

##var app = exports = module.exports = {};

其实,当咱们了解到原理后,不难明白这样的写法有点冗余,实际上是为了保证,模块的初始化环境是干净的。同时也方便咱们,即便改变了 module.exports 指向的对象后,依然能沿用 exports的特性

 exports = module.exports = createApplication;

 /**
  * Expose mime.
  */

 exports.mime = connect.mime;
 

例子,当中module.exports = createApplication改变了module.exports了,让exports失效,经过exports = module.exports的方法,让其恢复原来的特色。

##exports.init= function(){}

这种最简单,直接就是导出模块 init的方法。

##var mongoose = module.exports = exports = new Mongoose;

集多功能一身,不过根据上文所描述的,你们应该不能得出答案。

相关文章
相关标签/搜索