module.export和export
- module.exports 和 exports 是引用到的同一个对象,类似下面代码所示(为了举例,不是完全的正确):
var module.exports = {};
var exports = module.exports;
在模块内,没对module.exports和exports做任何更改前,他们都是指向同一个空对象。
- 在其他模块内使用require引用的是module.exports 对象,不一定是exports指向的引用对象,这点在开发中很容易因为忽略而导致bug产生。如下例子:
exports = function(){};
//在其他模块内
require(...);//得到{}
此时在其他模块内使用require引用上面模块导出的对象时,发现为空对象,为什么?因为使用exports=function(){}时,是将exports的引用指向了这个函数,而module.exports的引用还是空对象,并且其 他模块使用require引用的是module.exports,所以此处会得到空对象,而不是函数。当然此处写成module.exports = function(){}肯定是没问题的。
- 有人说为了防止上面的bug产生,一般都执行下 exports = module.exports,,但是直接覆盖module.exports不是一个好习惯,所以对于 module.exports = {a:123, b:456} 这样的情况,分开来写:
exports.a = 123;
exports.b = 456;
- 下面贴下几种情况,其他模块得到的不同结果:
exports = function fn(){}; // outputs "@routes {}"
exports.fn = function fn(){}; // outputs "@routes { fn: [Function: fn] }"
module.exports = function fn(){}; // outputs "@routes function fn(){}"
module.exports.fn = function fn(){}; // outputs "@routes { fn: [Function: fn] }"
module.export和export的更多相关文章
- module.exports与exports,export和export default
还在为module.exports.exports.export和export default,import和require区别与联系发愁吗,这一篇基本就够了! 一.首先搞清楚一个基本问题: modu ...
- module.exports,exports,export和export default,import与require区别与联系【原创】
还在为module.exports.exports.export和export default,import和require区别与联系发愁吗,这一篇基本就够了! 一.首先搞清楚一个基本问题: modu ...
- Node.js模块导出module.exports 和 exports,Es6模块导出export 和export default的区别
1.module.exports module变量代表当前模块.这个变量是一个对象,module对象会创建一个叫exports的属性,这个属性的默认值是一个空的对象: module.exports ...
- exports与module.exports的区别,export与export.defult区别
在JS模块化编程中,之前使用的是require.js或者sea.js.随着前端工程化工具webpack的推出,使得前端js可以使用CommonJS模块标准或者使用ES6 moduel特性. 在Comm ...
- export与export default exports与module.exports的用法
转载:http://blog.csdn.net/zhou_xiao_cheng/article/details/52759632 本文原创地址链接:http://blog.csdn.net/zhou_ ...
- module.exports 、exports、export、export default的区别
module.exports和exports是属于 CommonJS 模块规范,export和export default是属于ES6语法. module.exports和exports导出模块,用r ...
- module.exports,exports,export和export default,import与require区别与联系
还在为module.exports.exports.export和export default,import和require区别与联系发愁吗,这一篇基本就够了! 一.首先搞清楚一个基本问题: modu ...
- export、export default、module.export区别
在es6里面定义模块,导出模块时可以使用export.export default 这2者区别: 在同一个文件里面可以有多个export, 一个文件里面只能有1个export default //a. ...
- export,export default,module.exports,import,require之间的区别和关联
module.exports Node 应用由模块组成,采用 CommonJS 模块规范.根据这个规范,每个文件就是一个模块,有自己的作用域.在这些文件里面定义的变量.函数.类,都是私有的,对外不可见 ...
- exports、module.exports 和 export、export default
先了解他们的使用范围. require: node 和 es6 都支持的引入export / import : 只有es6 支持的导出引入module.exports / exports: 只有 no ...
随机推荐
- Orchard分类Taxonomies图文教程
Orchard分类和标签都实现对内容的分类管理,两者区别是分类的子项之间是具有级别(同级.上下级)关系,而标签是很随意的,子项之间可以有关系也可以没有,今天给大家分享分类的使用方法. 一.环境说明 O ...
- php:微信公众号token验证失败原因、验证码显示不出来的问题
ob_clean(); 问题描述: 用微信官方提供的demo验证token是成功的,但是放到自己网站的框架上进行token验证老是提示"token验证失败",经过检查(用生成日志的 ...
- c语言warning总结
1.function declaration isn’t a prototype括号中无参数,也要加void 2.array subscript is above array bounds数组下标大于 ...
- js 动态添加行,删除行,并获得select中值赋予 input
<html> <head> <title>Ace Test</title> <script type="text/javascrip ...
- js中var self=this的解释
每个函数在定义被ECMAScript解析器解析时,都会创建两个特殊的变量:this和arguments,换句话说,每个函数都有属于自己的this对象,这个this对象是在运行时基于函数的执行环境绑定的 ...
- crc循环冗余校验
循环冗余校验(Cyclic Redundancy Check, CRC)是一种根据网络数据包或电脑文件等数据产生简短固定位数校验码的一种散列函数,主要用来检测或校验数据传输或者保存后可能出现的错误.它 ...
- PLSQL win7 64位
1. 解压instantclient-basic-win32-11.2.0.1.0.zip至Oracle安装目录的Product下 具体目录如下D:\Oracle\product\instantcli ...
- mysql中的SUBSTRING_INDEX
SUBSTRING_INDEX(str,delim,count) Returns the substring from string str before count occurrences of t ...
- Bootstrap Table Examples
The examples of bootstrap table http://bootstrap-table.wenzhixin.net.cn/examples/ http://www.jq22.co ...
- Android学习---如何创建数据库,SQLite(onCreate,onUpgrade方法)和SQLiteStudio的使用
一.android中使用什么数据库? SQLite是遵守ACID的关系数据库管理系统,它包含在一个相对小的C程式庫中.它是D.RichardHipp建立的公有领域项目.SQLite 是一个软件库,实现 ...