nodejs -- require , exports , module
1. require , exports .

--------------------------

文件:
1) index.js
//两种方式都可以:
var forExports = require("./forExports");
// var forExports = require("./forExports.js");
var forExports2 = require("./forExports2.js"); //下面两种方式都可以:
// var data = require("./data.json");
var data = require("./data"); forExports.say("tom", "i want to eat beef"); forExports2.say("lucy", "i want to shop"); console.log(data);
2)data.json
{
"name": "BeJson",
"url": "http://www.bejson.com",
"page": 88,
"isNonProfit": true,
"address": {
"street": "科技园路.",
"city": "江苏苏州",
"country": "中国"
},
"links": [
{
"name": "Google",
"url": "http://www.google.com"
},
{
"name": "Baidu",
"url": "http://www.baidu.com"
},
{
"name": "SoSo",
"url": "http://www.SoSo.com"
}
]
}
3)forExports.js
function say(who, what){
console.log(who + " 说: " + what+ "! [ from forExports.js ]");
}
exports.say = say;
4)forExports2.js
exports.say = function(who, what){
console.log(who + " 说: " + what+ "! [from forExports2.js]");
};
运行:

----------------------
分析:
第一: 两个模块 都有 方法say. 并且 say 方法 都 赋值给了 本模块的导出对象 .(这两个导出对象是不影响的)

第二: require引入 模块的时候,后缀可以省略 . ./data.json 和 ./data 都可以.

第三: exports导出对象 赋予的是一个方法.
2. module

1)上面虽然被替换成一个函数, 但是 仍然可以构造函数使用 . 此时实例化 ,可以输出 hello world.
2)可以当做 普通函数进行使用
------------------------------------------------
代码:
1. index.js
/**********require,exports的使用*************/ //两种方式都可以:
// var forExports = require("./forExports");
// // var forExports = require("./forExports.js");
// var forExports2 = require("./forExports2.js"); //下面两种方式都可以:
// var data = require("./data.json");
// var data = require("./data"); // forExports.say("tom", "i want to eat beef"); // forExports2.say("lucy", "i want to shop"); // console.log(data); /***********module使用:***************/ //1-: 实名的构造函数
var Person = require('./forModule.js');
var lucy = new Person("lucy", 23, "shopping");
console.log(lucy);
lucy.like(); //2-: 匿名的构造函数
var Person2 = require("./forModule2.js");
var liming = new Person2("liming", 20, "football");
liming.like(); //3-: 一个普通的函数, 但是也是当做构造函数,必须实例化,才有输出:
var Person3 = require("./forModule3.js");
var jim = new Person3("jim", 16, "swimming"); //4-: 一个普通的函数, 当做普通函数:
var Person4 = require("./forModule3.js");
//调用函数:
Person4("Jack", 30, "cooking");
2. forModule.js : 有名字的构造函数
function Person (name, age, action) {
this.name = name;
this.age = age;
this.like = function(){
console.log(name+ ",年龄:"+age+";喜欢[" +action+ "]");
};
}
module.exports = Person;
3. forModule2.js 匿名的构造函数:
module.exports = function(name, age, action) {
this.name = name;
this.age = age;
this.like = function(){
console.log(name+ ",年龄:"+age+";喜欢[" +action+ "]");
};
};
4. forModule3.js 一个普通的函数, 但是仍然 当做是 构造函数使用. 或者 当做普通函数使用.
module.exports = function(name, age, action) {
console.log(name+ ",年龄:"+age+";喜欢[" +action+ "]");
};
--------------------------------
运行:

-------------------------------------
分析:
对于 forModule3.js ,
1)尽管只是一个普通函数:
module.exports = function(name, age, action) {
console.log(name+ ",年龄:"+age+";喜欢[" +action+ "]");
};
但是,仍然是当做构造函数, 进行 实例化 ,才会有输出:
//3-: 一个普通的函数, 但是也是当做构造函数,必须实例化,才有输出:
var Person3 = require("./forModule3.js");
var jim = new Person3("jim", 16, "swimming");
控制台输出:

2)当做一个普通的函数. 此时 require引入的就是一个函数.
//4-: 一个普通的函数, 当做普通函数:
var Person4 = require("./forModule3.js");
//调用函数:
Person4("Jack", 30, "cooking");
控制台输出:

---------------------------------
参考链接:
nodejs -- require , exports , module的更多相关文章
- require exports module.exports
require 用于引入模块(js文件). JSON.或本地文件 自己写的模块必须是相对路径,省略了node就认为该自定义模块(js文件)是核心模块(内置模块或者第三方模块) node 有模块作用域 ...
- require/exports 和 import/export 区别
零.区别 1.require/exports 是 CommonJS 的标准,适用范围如 Node.js 2.import/export 是 ES6 的标准,适用范围如 React 一.间接获取对象 ( ...
- nodejs里的module.exports和exports
引 在node.js中我们可以使用module.exports和exports导出模块,设置导出函数.数组.变量等等 为什么可以用这两个模块? 或者直接问,node.js的模块功能是怎么实现的. 这样 ...
- nodejs中的require,exports使用说明
模块是一门语言编写大项目的基石,因此,了解如何组织.编写.编译.加载模块很重要.这里主要谈谈Node中的模块加载. 1.Node中的模块,主要使用require来加载模块,文件 require(&qu ...
- nodejs里的module.exports和exports的关系
关于node里面的module.exports和exports的异同,网上已经有很多的资料,很多的文章,很多的博客,看了很多,好像懂了,又好像不懂,过几天又不懂了...大致总结是这样的: //下面这种 ...
- 【nodejs】exports 和 module.exports 的区别
require 用来加载代码,而 exports 和 module.exports 则用来导出代码.但很多新手可能会迷惑于 exports 和 module.exports 的区别,为了更好的理解 e ...
- NodeJS的exports、module.exports与ES6的export、export default深入详解
前言 决定开始重新规范的学习一下node编程.但是引入模块我看到用 require的方式,再联想到咱们的ES6各种export .export default. 阿西吧,头都大了.... 头大完了,那 ...
- nodejs require
The rules of where require finds the files can be a little complex, but a simple rule of thumb is th ...
- nodeJS中exports和mopdule.exports的区别
每一个node.js执行文件,都自动创建一个module对象,同时,module对象会创建一个叫exports的属性,初始化的值是 {} module.exports = {}; Node.js为了方 ...
随机推荐
- Java并发——Fork/Join框架与ForkJoinPool
为了防止无良网站的爬虫抓取文章,特此标识,转载请注明文章出处.LaplaceDemon/ShiJiaqi. http://www.cnblogs.com/shijiaqi1066/p/4631466. ...
- 20175208 《Java程序设计》第四周学习总结
第五章主要学习内容 1.子类的继承性: (1)子类与父类在同一包中的继承性:子类自然地继承了其父类中不是private的成员变量作为自己的成员变量. (2)子类与父类不在同一包中的继承性:子类只继承父 ...
- php window系统 xdebug+phpstorm 本地断点调试使用教程
运行环境: phpStorm 2017.2 PHP 7.1.5 Xdebug 2.6.1 php.ini添加xdebug模块 你需要仔细分析和选择要下载的对应版本,否则无法调试.由于非常容易出错,建议 ...
- cron笔记
以前以为添加计划任务就是crontab -e来添加,知道今天偶然发现了/etc/cron.d目录,才发现事情没有那么简单.. crontab -e命令编辑的文件是保存在/var/spool/cron/ ...
- Weighted Quick Union
Weighted Quick Union即: 在Quick Union的基础上对结点加权(weighted),在parent[i]基础上增加一个size[i]. 用来存储该结点(site)的所有子结点 ...
- Code Pages
https://docs.microsoft.com/en-us/windows/desktop/intl/code-pages Most applications written today han ...
- LOJ #10132. 「一本通 4.4 例 3」异象石
题目地址 LOJ 题解 神仙思路.思路参考自<算法竞赛进阶指南>. 考虑维护dfs序中相邻两个石头的距离,那么每次?的答案就是sum/2(首尾算相邻) 然后维护一下拿个平衡树/set维护一 ...
- Learning-MySQL【5】:数据的操作管理
一.插入数据 1.为表的所有字段插入数据 通常情况下,插入的新纪录要包含表的所有字段 INSERT 语句有两种方式可以同时为表的所有字段插入数据,第一种方式是不指定具体的字段名,第二种方式是列出表的所 ...
- 【Visual Studio 扩展工具】使用 ComponentOne迷你图控件,进行可视化数据趋势分析
概述 迷你图 —— Sparklines是迷你的轻量级图表,有助于快速可视化数据. 它们是由数据可视化传奇人物Edward Tufte发明的,他将其描述为“数据密集,设计简单,字节大小的图形.”虽然迷 ...
- jsp/servlet学习三之会话管理初解
由于http的无状态性,使得会话管理或会话跟踪成为web应用开发一个无可避免的主题.默认下,一个web服务器无法区分一个http请求是否为第一次访问.例如,一个web邮件应用要求用户登陆后才能查看邮件 ...