Node.js之exports与module.exports
每一个node.js执行文件,都自动创建一个module对象,同时,module对象会创建一个叫exports的属性,初始化的值是 {}
module.exports = {};
Node.js为了方便地导出功能函数,node.js会自动地实现以下这个语句
tool.js
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,而不是exports
再看看下面例子
hammer.js
exports.a = function(){
console.log('a')
} module.exports = {a: 2}
exports.a = 1
test.js
var x = require('./hammer'); console.log(x.a)
result:
2
exports在module.exports 被改变后,失效。
是不是开始有点廓然开朗,下面将会列出开源模块中,经常看到的几个使用方式。
##module.exports = View
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;
集多功能一身,不过根据上文所描述的,大家应该不能得出答案。
Node.js之exports与module.exports的更多相关文章
- Node.js中exports,module.exports以及require方法
在Node.js中,使用module.exports.f = ...与使用exports.f = ...是一样的,此时exports就是module.exports的一种简写方式.但是,需要注意的是, ...
- Node.js中exports与module.exports的区别
原文:http://www.hacksparrow.com/node-js-exports-vs-module-exports.html 你肯定对Node.js模块中用来创建函数的exports对象很 ...
- Node.js exports与module.exports的关系
今天搜索module.exports时看到CNode社区上发的Hack Sparrow一篇相关文章的链接 Node.js Module – exports vs module.exports 一篇5年 ...
- (译)Node.js的模块-exports和module.exports
原文标题:Node.js Module – exports vs module.exports 原文链接:http://www.hacksparrow.com/node-js-exports-vs-m ...
- Node.js模块导出exports 和 module.exports 的区别
原文: https://blog.csdn.net/Pwiling/article/details/51958693 每一个node.js执行文件,都自动创建一个module对象,同时,module对 ...
- node.js中的exports和module.exports
不同的编程语言都有各自的代码组织和复用的方式,如.net.php中的命名空间,python中的import,ruby中的module等,来避免命名空间污染.一直都没搞清楚node中的exports和m ...
- Node.js学习之(第二章:exports和module.exports)
前言 Node中,每个模块都有一个exports接口对象,我们需要把公共的方法或者字符串挂载在这个接口对象中,其他的模块才可以使用. Node.js中只有模块作用域,默认两个模块之间的变量,方法互不冲 ...
- node.js模块中exports和module.exports的区别
Node应用由模块组成,采用CommonJS模块规范. 根据这个规范,每个文件就是一个模块,有自己的作用域.在一个文件里面定义的变量.函数.类,都是私有的,对其他文件不可见. CommonJS规范规定 ...
- Node.js中的exports与module.exports的区分
1. module应该是require方法中,上下文中的对象 2. exports对象应该是上下文中引用module.exports的新对象 3. exports.a = xxx 会将修改更新到mod ...
随机推荐
- python dict conver json
demo: import jsonimport requestsimport xml.etree.ElementTree as et def xmlsjondemo(): data={'statusc ...
- python Anaconda 安装管理包,开发环境
在自己的电脑上安装Anaconda,用conda create创建一个python 2.7版本的environment.今后我们的程序都在这个环境下执行 0.download anaconda and ...
- A. Counterexample (Codeforces Round #275(div2)
A. Counterexample time limit per test 1 second memory limit per test 256 megabytes input standard in ...
- Java Web项目中使用Freemarker生成Word文档
Web项目中生成Word文档的操作屡见不鲜.基于Java的解决方式也是非常多的,包含使用Jacob.Apache POI.Java2Word.iText等各种方式,事实上在从Office 2003開始 ...
- 【Unity笔记】静态碰撞体的陷阱
概念 静态碰撞体(Static Collider):物体勾选为静态Static,有Collider组件,无Rigidbody组件. 静态碰撞体的陷阱 Unity在游戏初始化时,会把所有的静态碰撞体合并 ...
- Java API学习(一) ArrayList源码学习
ArrayList在平常用的还挺多的,用起来十分舒服,顺手.这里来学习一下它的源码. 类定义 下面是类的定义: public class ArrayList<E> extends Abst ...
- zookeeper学习:知识点收集
其实学习zookeeper挺简单的,找一本书或者网上的资源,按照其中的例子做一遍就大致了解了.之前是自己学习的方法有问题. 1. 会启动单机版的服务器,并使用客户端连接,然后进行节点的各种操作 2. ...
- Spark SQL讲解
Spark SQL讲解 Spark SQL是支持在Spark中使用Sql.HiveSql.Scala中的关系型查询表达式.它的核心组件是一个新增的RDD类型SchemaRDD,它把行对象用一个Sche ...
- IIS 7上部署PHP【后续一】
在之前成功在windows 2008+iis7环境下部署php的基础上,今天把之前的挂Q网站转移到了这个服务器下. 文件拷贝到服务器后,问题继续出现. 首先出现的问题是,Mysql的数据库名称和账户密 ...
- Spark的机器学习算法mlib的例子运行
Spark自带了机器学习的算法mlib,页面网址 http://spark.incubator.apache.org/docs/latest/mllib-guide.html 但是运行的时候,遇到了很 ...