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导出对象 赋予的是一个方法.

  

  下载文件: 密码: hu3a

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");

控制台输出:

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

参考链接:

      代码下载: 密码: tdjv

nodejs -- require , exports , module的更多相关文章

  1. require exports module.exports

    require 用于引入模块(js文件). JSON.或本地文件 自己写的模块必须是相对路径,省略了node就认为该自定义模块(js文件)是核心模块(内置模块或者第三方模块) node  有模块作用域 ...

  2. require/exports 和 import/export 区别

    零.区别 1.require/exports 是 CommonJS 的标准,适用范围如 Node.js 2.import/export 是 ES6 的标准,适用范围如 React 一.间接获取对象 ( ...

  3. nodejs里的module.exports和exports

    引 在node.js中我们可以使用module.exports和exports导出模块,设置导出函数.数组.变量等等 为什么可以用这两个模块? 或者直接问,node.js的模块功能是怎么实现的. 这样 ...

  4. nodejs中的require,exports使用说明

    模块是一门语言编写大项目的基石,因此,了解如何组织.编写.编译.加载模块很重要.这里主要谈谈Node中的模块加载. 1.Node中的模块,主要使用require来加载模块,文件 require(&qu ...

  5. nodejs里的module.exports和exports的关系

    关于node里面的module.exports和exports的异同,网上已经有很多的资料,很多的文章,很多的博客,看了很多,好像懂了,又好像不懂,过几天又不懂了...大致总结是这样的: //下面这种 ...

  6. 【nodejs】exports 和 module.exports 的区别

    require 用来加载代码,而 exports 和 module.exports 则用来导出代码.但很多新手可能会迷惑于 exports 和 module.exports 的区别,为了更好的理解 e ...

  7. NodeJS的exports、module.exports与ES6的export、export default深入详解

    前言 决定开始重新规范的学习一下node编程.但是引入模块我看到用 require的方式,再联想到咱们的ES6各种export .export default. 阿西吧,头都大了.... 头大完了,那 ...

  8. nodejs require

    The rules of where require finds the files can be a little complex, but a simple rule of thumb is th ...

  9. nodeJS中exports和mopdule.exports的区别

    每一个node.js执行文件,都自动创建一个module对象,同时,module对象会创建一个叫exports的属性,初始化的值是 {} module.exports = {}; Node.js为了方 ...

随机推荐

  1. 星型数据仓库olap工具kylin介绍和简单使用示例

    本文转载自:https://www.cnblogs.com/hsydj/p/4515057.html 星型数据仓库olap工具kylin介绍 星型数据仓库olap工具kylin介绍 数据仓库是目前企业 ...

  2. Oarcle 入门之where关键字

    --where 关键字 --作用:过滤行 *将需要的行用where引导出来 用法: 1.判断符号:=,!=,<>,<,>,<=,>=,any,some,all; 例 ...

  3. Docker Macvlan 介绍 or 工作原理

    Docker Macvlan Network Macvlan Network:属于Docker的网络驱动. Macvlan Network:Docker主机网卡接口逻辑上分为多个子接口,每个子接口标识 ...

  4. cron笔记

    以前以为添加计划任务就是crontab -e来添加,知道今天偶然发现了/etc/cron.d目录,才发现事情没有那么简单.. crontab -e命令编辑的文件是保存在/var/spool/cron/ ...

  5. luogu[愚人节题目3]现代妖怪殖民地 NTT

    U34272 [愚人节题目3]现代妖怪殖民地 fft 题目链接 https://www.luogu.org/problemnew/show/U34272 思路 虽然是个py题. ntt(或者fft)模 ...

  6. FastAdmin笔记~

    在phpstud命令行输入以下命令! 1.在命令行一键生成test表格的crud php think crud -t  test 2.在数据库创建表必须加上表注释 3.生成test控制器的权限菜单 p ...

  7. Vue:(二)基础常用语法

    (一)模板语法 Mustache语法:{{ msg }} Html赋值:v-html = " " 绑定属性:v-bind:id = " " 使用表达式:{{ o ...

  8. Codeforces 587 E. Duff as a Queen

    题目链接:http://codeforces.com/contest/587/problem/E 其实就是线段树维护区间线性基,合并的时候注意一下.复杂度${O(nlog^{3})}$ #includ ...

  9. 一招破解 Java 集合类面试题

    今日招式:Java集合类面试题 Java集合类绝对是我们的老朋友了,Java技术江湖里,谁人不知,谁人不晓,它的使用率非常高,使用难度却也不大,这也导致了很多人对它不屑一顾,殊不知其中却暗藏玄机,今天 ...

  10. 【JMeter】教程及技巧汇总(转载)

    转载地址:http://www.hissummer.com/jmeter-summary.html 参考/学习资料:http://www.yiibai.com/jmeter/jmeter_build_ ...