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. 文件夹放到VS2015显示不出来

    在该项目中,点这个按钮 , 会显示没有包括进项目的文件 在要包括的文件上右键--包括在项目中即可 同理也可以从项目排除

  2. OAuth 2.0之授权码模式

    转载自:http://www.ruanyifeng.com/blog/2014/05/oauth_2_0.html OAuth 2.0授权码模式 授权码模式(authorization code)是功 ...

  3. vue解决启动报错cjs loader.js Error: Cannot find module '../config'问题

    vue解决启动报错cjs loader.js Error: Cannot find module '../config'问题 今天下载了一个开源项目一直运行不了,折腾了半天才找到问题所在,config ...

  4. jQuery属性attr

    设置多个属性/值对 为被选元素设置一个以上的属性和值. 语法 $(selector).attr({attribute:value, attribute:value ...})比如:$("im ...

  5. jquery改变字符串中部分字符的颜色

    //该方法改变字符串中中括号内(包括中括号)的字符串颜色为红色function changecolocer() { var zf = $('#YWFA').text(); if(zf.length&g ...

  6. 详解 Nginx如何配置Web服务器

    概述 在高层次上,将NGINX配置作为Web服务器有一些问题需要了解,定义它处理哪些URL以及如何处理这些URL上的资源的HTTP请求. 在较低层次上,配置定义了一组控制对特定域或IP地址的请求的处理 ...

  7. 如何退出vim

    按ESC键 按ESC键 按ESC键 然后: 最下面出现一条能输入命令的地方 输入冒号 输入冒号 输入冒号 然后输入命令: :w 保存文件但不退出 :w file 将修改另外保存到 file 中,不退出 ...

  8. java基础语法2.

    第二章 2.1 class文件的生成 java文件为源代码文件 class为程序. class文件实时修改. eclipse自动生成. project下面clean. 2.2 jar文件 如何将有用的 ...

  9. Bytom合约预编译

    比原项目仓库: Github地址:https://github.com/Bytom/bytom Gitee地址:https://gitee.com/BytomBlockchain/bytom 在开发合 ...

  10. Learning-Python【18】:Python常用模块(1)—— time、datetime、randrom

    time 模块:与时间相关的功能的模块 在 Python 中,时间分为三种: 1.时间戳:是一个时间的表示,根据不同的语言,可以是整数或浮点数,是从1970年1月1日0时0分0秒到现在经历的秒数 2. ...