nodejs -- require , exports , module

 

 

1. require , exports .

 

 

 

--------------------------json

 

 

 

 

 

 

 

 

 

 

 

文件:函數

1) index.jsui

 1 //兩種方式均可以:
 2 var forExports = require("./forExports");
 3 // var forExports = require("./forExports.js");
 4 var forExports2 = require("./forExports2.js");
 5 
 6 //下面兩種方式均可以:
 7 // var data = require("./data.json");
 8 var data = require("./data");
 9 
10 forExports.say("tom", "i want to eat beef");
11 
12 forExports2.say("lucy", "i want to shop");
13 
14 console.log(data);

 

2)data.jsonthis

 1 {
 2     "name": "BeJson",
 3     "url": "http://www.bejson.com",
 4     "page": 88,
 5     "isNonProfit": true,
 6     "address": {
 7         "street": "科技園路.",
 8         "city": "江蘇蘇州",
 9         "country": "中國"
10     },
11     "links": [
12         {
13             "name": "Google",
14             "url": "http://www.google.com"
15         },
16         {
17             "name": "Baidu",
18             "url": "http://www.baidu.com"
19         },
20         {
21             "name": "SoSo",
22             "url": "http://www.SoSo.com"
23         }
24     ]
25 }

 

3)forExports.jsgoogle

1 function say(who, what){
2     console.log(who + " 說: " + what+ "! [ from forExports.js ]");
3 }
4 
5 exports.say = say;

 

4)forExports2.jsurl

1 exports.say = function(who, what){
2     console.log(who + " 說: " + what+ "! [from forExports2.js]");
3 };

 

 

 運行:spa

 

 ----------------------3d

分析:code

  第一: 兩個模塊 都有 方法say.    而且 say 方法 都 賦值給了 本模塊的導出對象 .(這兩個導出對象是不影響的)對象

    

  

  第二: require引入 模塊的時候,後綴能夠省略 .   ./data.json  和 ./data 均可以.

    

 

   

  第三: exports導出對象 賦予的是一個方法.

  

 

   下載文件: 密碼: hu3a 

 

 2. module

 

1)上面雖然被替換成一個函數, 可是 仍然能夠構造函數使用 . 此時實例化 ,能夠輸出  hello world.

2)能夠當作 普通函數進行使用

 

 

 

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

代碼:

1. index.js

 

 1 /**********require,exports的使用*************/
 2 
 3 //兩種方式均可以:
 4 // var forExports = require("./forExports");
 5 // // var forExports = require("./forExports.js");
 6 // var forExports2 = require("./forExports2.js");
 7 
 8 //下面兩種方式均可以:
 9 // var data = require("./data.json");
10 // var data = require("./data");
11 
12 // forExports.say("tom", "i want to eat beef");
13 
14 // forExports2.say("lucy", "i want to shop");
15 
16 // console.log(data);
17 
18 
19 
20 
21 /***********module使用:***************/
22 
23 //1-: 實名的構造函數
24 var Person = require('./forModule.js');
25 var lucy = new Person("lucy", 23, "shopping");
26 console.log(lucy);
27 lucy.like();
28 
29 //2-: 匿名的構造函數
30 var Person2 = require("./forModule2.js");
31 var liming = new Person2("liming", 20, "football");
32 liming.like();
33 
34 //3-: 一個普通的函數, 可是也是當作構造函數,必須實例化,纔有輸出:
35 var Person3 = require("./forModule3.js");
36 var jim = new Person3("jim", 16, "swimming");
37 
38 //4-: 一個普通的函數, 當作普通函數:
39 var Person4 = require("./forModule3.js");
40 //調用函數:
41 Person4("Jack", 30, "cooking");

 

 

 

 2. forModule.js  : 有名字的構造函數

1 function Person (name, age, action) {
2     this.name = name;
3     this.age = age;
4     this.like = function(){
5         console.log(name+ ",年齡:"+age+";喜歡[" +action+ "]");
6     };
7 }
8 
9 module.exports = Person;

 

 

3. forModule2.js 匿名的構造函數:

 

1 module.exports = function(name, age, action) {
2     this.name = name;
3     this.age = age;
4     this.like = function(){
5         console.log(name+ ",年齡:"+age+";喜歡[" +action+ "]");
6     };
7 };

 

 

 4. forModule3.js 一個普通的函數, 可是仍然 當作是 構造函數使用. 或者 當作普通函數使用.

 

1 module.exports = function(name, age, action) {
2     console.log(name+ ",年齡:"+age+";喜歡[" +action+ "]");
3 };

 

 

 

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

運行:

 

 

 

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

分析:

對於 forModule3.js ,

1)儘管只是一個普通函數:

1 module.exports = function(name, age, action) {
2     console.log(name+ ",年齡:"+age+";喜歡[" +action+ "]");
3 };

 

可是,仍然是當作構造函數, 進行 實例化 ,纔會有輸出:

1 //3-: 一個普通的函數, 可是也是當作構造函數,必須實例化,纔有輸出:
2 var Person3 = require("./forModule3.js");
3 var jim = new Person3("jim", 16, "swimming");

 

控制檯輸出:

 

 

2)當作一個普通的函數. 此時 require引入的就是一個函數.

1 //4-: 一個普通的函數, 當作普通函數:
2 var Person4 = require("./forModule3.js");
3 //調用函數:
4 Person4("Jack", 30, "cooking");

 

控制檯輸出:

 

 

 

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

 

參考連接:

      代碼下載: 密碼: tdjv

相關文章
相關標籤/搜索