node——module.exports
module.exports

1.
在a.js中
var b=require('./b.js');
console.log(b);
在b.js中
function add(x,y){
return x+y;
}
var result=add(100,1000);
console.log(result);
执行a.js

当加载一个模块,默认被require()加载后,返回的是一个对象{}
2.
在b.js中
function add(x,y){
return x+y;
}
var result=add(100,1000);
console.log(result);
//return "hello";会有问题
//给module.exports符什么值,加载b.js模块的时候就会返回什么值
module.exports='hello world!';
执行a.js

给module.exports符什么值,加载b.js模块的时候就会返回什么值
3.
b.js
function add(x,y){
return x+y;
}
var result=add(100,1000);
console.log(result);
//return "hello";会有问题
//给module.exports符什么值,加载b.js模块的时候就会返回什么值
//module.exports='hello world!';//字符
//module.exports=32233;//数字
module.exports=function(x){
console.log(x);
};
a.js
var b=require('./b.js');
console.log(b);
b('hahaha');
执行a.js

module.exports后面可以符字符串,数值,还有函数
4.
那么我们还可以这样
b.js
function add(x,y){
return x+y;
}
var result=add(100,1000);
console.log(result);
//return "hello";会有问题
//给module.exports符什么值,加载b.js模块的时候就会返回什么值
//module.exports='hello world!';//字符
//module.exports=32233;//数字
/*module.exports=function(x){
console.log(x);
};*/
module.exports.name='Jim';
module.exports.age=11;
module.exports.show=function(){
console.log(this.name+this.age);
}
a.js
var b=require('./b.js');
console.log(b.name);
console.log(b.age);
console.log(b.show);
b.show();
执行a.js

b.js也可以返回出这样的对象
总结:
所以,require用来加载模块,module.exports用来暴露模块
module.export与export的区别
a.js
var b=require('./b.js');
console.log(b.name);
console.log(b.age);
b.show();
b.js
module.exports.name='Bob';
exports.age=12;
exports.show=function(){
console.log(this.name+this.age);
}
执行a.js

得到的结果与module.export的1结果相同,但是module.export与export之间还是有些差别的,我们来看下面的例子
a.js
var b=require('./b.js');
console.log(b);
console.log(b.name);
console.log(b.age);
b.show();
b('hahaha');
b.js
module.exports.name='Bob';
exports.age=12;
exports.show=function(){
console.log(this.name+this.age);
};
module.exports='Hello !';
执行a.js

可以看出最后暴露出的只有module.exports=“Hello !”,而exports的都没有暴露出来
原因:
因为module.exports和exports相当于一个栈里的两个变量,module.exports先指向堆里的一个对象,给对里添加了一个name属性

exports会和module.exports指向同一个对象,添加属性

之后如果又对module.export赋值:module.exports=‘Hello !’;,这个时候module.exports会指向堆里的一个新的地方

exports就没有和module.exports在同一个对象里了,而会返回的值会是module.exports的值,所以exports不会暴露出来
再修改一下b.js
module.exports.name='Bob';
exports.age=12;
exports.show=function(){
console.log(this.name+this.age);
};
exports='Hello !';

可以看出,最终返回的还是module.exports
exports存在因为它是一个快捷方式,是为了我们使用更方便
node——module.exports的更多相关文章
- 探讨ES6的import export default 和CommonJS的require module.exports
今天来扒一扒在node和ES6中的module,主要是为了区分node和ES6中的不同意义,避免概念上的混淆,同时也分享一下,自己在这个坑里获得的心得. 在ES6之前 模块的概念是在ES6发布之前就出 ...
- 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年 ...
- es6 import export 与 node 中的module.exports exports
1.export a.export 变量 export var name = 'jack';export var age = 18;//等同于 var name = 'jack';var age = ...
- Node.js module.exports和exports的区别
require 用来加载代码,而 exports 和 module.exports 则用来导出代码,从接触node.js就不会它们两陌生,上代码: foo.js exports.a = functio ...
- Node中Exports与module.export的使用与区别
最近在看<node开发实战详解>时有写疑问,所以自己就整理了一些资料.下面是node4.*的官方api文档(http://nodejs.cn/doc/node_4/modules.html ...
- (译)Node.js的模块-exports和module.exports
原文标题:Node.js Module – exports vs module.exports 原文链接:http://www.hacksparrow.com/node-js-exports-vs-m ...
- Node.js模块导出module.exports 和 exports,Es6模块导出export 和export default的区别
1.module.exports module变量代表当前模块.这个变量是一个对象,module对象会创建一个叫exports的属性,这个属性的默认值是一个空的对象: module.exports ...
- node (02 CommonJs 和 Nodejs 中自定义模块)顺便讲讲module.exports和exports的区别 dependencies 与 devDependencies 之间的区别
CommonJS 规范的提出,主要是为了弥补当前 JavaScript 没有标准的缺陷.它的终极目标就是:提供一个类似 Python,Ruby 和 Java 语言的标准库,而不只是停留在小脚本程序的阶 ...
随机推荐
- node——通过express模拟Apache实现静态资源托管
1.express中处理静态资源的函数 创建一个app.js作为入口文件,创建一个public文件夹作为静态资源文件夹 var app=express();var fn=express.static( ...
- js常用正则表达式大全--如:数字,字符等
一.校验数字的表达式 1 数字:^[0-9]*$ 2 n位的数字:^\d{n}$ 3 至少n位的数字:^\d{n,}$ 4 m-n位的数字:^\d{m,n}$ 5 零和非零开头的数字:^(0|[1-9 ...
- C++进阶 STL(3) 第三天 函数对象适配器、常用遍历算法、常用排序算法、常用算数生成算法、常用集合算法、 distance_逆序遍历_修改容器元素
01昨天课程回顾 02函数对象适配器 函数适配器是用来让一个函数对象表现出另外一种类型的函数对象的特征.因为,许多情况下,我们所持有的函数对象或普通函数的参数个数或是返回值类型并不是我们想要的,这时候 ...
- [luogu P1962] 斐波那契数列(带快速幂矩阵乘法模板)
题目背景 大家都知道,斐波那契数列是满足如下性质的一个数列: • f(1) = 1 • f(2) = 1 • f(n) = f(n-1) + f(n-2) (n ≥ 2 且 n 为整数) 题目描述 请 ...
- java基础口述
1:什么是变量?变量的定义格式?要使用变量需要注意什么? 在程序运行过程中,其值是可以在某个范围内发生改变的量. 变量其实就是内存中一小块区域. 由3部分组成: 1,数据类型: 限定变量的取值 2,变 ...
- CentOS 笔记(五) 常用工具
远程 :XShell6 ,PuTTy FPT:Xfpt ,pscp.exe
- ASP.NET - 单元测试
Assert类的使用 Assert.Inconclusive() 表示一个未验证的测试: Assert.AreEqual() 测试指定的值是否相等,如果相等,则测试通过: AreSame() 用于验证 ...
- [SharePoint][SharePoint Designer 入门经典]Chapter11 工作流基础
1.SPS中可以创建的工作流的种类 2.SPD工作流基础 3.创建列表\库工作流 4.创建可重用的工作流 5.利用基于站点的工作流 6.SPD 工作流的限制和注意事项
- 数论(同余+hash)
Time Limit:3000MS Memory Limit:65536KB Description You are given a sequence a[0]a[1] ... a[N-1] of d ...
- Web前端开发实战2:二级下拉式菜单之JS实现
上一篇博文提到了二级下拉式菜单是用HTML和CSS实现的.我们这一篇来用JavaScript脚本实现下拉菜单的显 示和隐藏. 使用 JavaScript方法实现我们须要用的知识有: 1)JS事件:on ...