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为了方 ...
随机推荐
- Vue用axios跨域访问数据
Vue用axios跨域访问数据axios是vue-resource的替代品,vue-resource不再维护.安装axios:npm install axios使用vue-cli开发时,由于项目本身启 ...
- qrCode二维码字符串长度太多压缩的问题
昨天整微信的扫码支付时,用qrcode生成二维码,结果字符串太长而失败.今天发现利用pako压缩,可扫描后显示乱码,特记录一下. palo插件地址:https://github.com/nodeca/ ...
- Python+OpenCV图像处理(十六)—— 轮廓发现
简介:轮廓发现是基于图像边缘提取的基础寻找对象轮廓的方法,所以边缘提取的阈值选定会影响最终轮廓发现结果. 代码如下: import cv2 as cv import numpy as np def c ...
- 破解网页右键被禁止js
按F12,点击console输入一下内容后按回车 javascript:alert(document.onselectstart = document.oncontextmenu= document. ...
- tomcat启动后8005端口未被占用
8005端口是tomcat本身的端口,如果这个端口在启动时未被tomcat占用的话,就无法使用它自带的shutdown.sh脚本关闭tomcat 接下来我以tomcat-9.0.12为例说明 下载to ...
- robot framework学习五——AutoltLibrary库
安装中遇到的问题: 安装好了AutoItLibrary,但是导入到RIDE后,仍然红色显示 搜索了下解决办法,说要安装下autoit-v3-setup.exe https://www.autoitsc ...
- [原]编译flightGear
参考:flightgear编译博客201705 flightGear是三维飞行仿真软件,这个款软件是开源的,我们尝试用其源码完整编译一遍这个工程,并使用它. 它用到里以下扩展库: 空气动力学库:JSB ...
- docker-compose控制启动顺序
用官方方案https://docs.docker.com/compose/startup-order/ 下载wait-for-it.sh https://github.com/vishnubob/wa ...
- PHP设计模式概念
设计模式(Design Pattern)是对软件设计中普遍存在(反复出现)的各种问题,所提出的解决方案 设计模式并不直接用来完成代码的编写,而是描述在各种不同情况下,要怎么解决问题的一种方案.面向对象 ...
- postman(八):使用newman来执行postman脚本
通过之前的了解,我们知道postman是基于javascript语言编写的,而导出的json格式的postman脚本也无法直接在服务器运行,它需要在newman中执行(可以把newman看做postm ...