函數調用
1. 文件內普通函數調用
創建一個js文件命名為2_callFunction.js,其中定義一個函數fun1,向返回對象輸出了一段字符串“你好,我是fun1”。
1 //--------------------2_callFunction.js--------------------------------- 2 var http = require('http'); 3 http.createServer(function (request, response) { 4 response.writeHead(200, {'Content-Type': 'text/html; charset=utf-8'}); 5 if(request.url!=="/favicon.ico"){ //清除第2此訪問 6 fun1(response);//調用普通函數fun1 7 response.end(); 8 } 9 }).listen(8000); 10 console.log('Server running at http://127.0.0.1:8000/'); 11 //---普通函數 12 function fun1(res){ 13 res.write("你好,我是fun1"); 14 }
我們運行:node 2_callFunction,打開瀏覽器
2. 調用其他文件里的函數
首先我們先創建一個otherfuns.js文件,這個文件里有兩個函數,call函數被controller調用,這兩個函數都向response對象輸出一段字符串,函數通過module.exports提供給外部調用。這里我們只提供對外一個函數controller
1 //-------------------models/otherfuns.js-------------------------- 2 function controller(res){ 3 res.write("執行controller <br>"); 4 call(res); 5 res.end(); 6 } 7 function call(res){ 8 res.write("執行call"); 9 } 10 module.exports = controller; //只支持一個函數
我們通過require將otherfuns.js引入到主文件里,require工作機制參見 require() 源碼解讀。
下面兩句都可以調用到controller函數:
1) var other =new otherfun (response);//otherfuns.js 里的方法controller被調用
2) otherfun (response);//otherfuns.js 里的方法controller被調用
1 //--------------------2_callFunction.js--------------------------------- 調用otherfuns.js里的函數 2 var http = require('http'); 3 var otherfun = require('./models/otherfuns.js'); 4 http.createServer(function (request, response) { 5 response.writeHead(200, {'Content-Type': 'text/html; charset=utf-8'}); 6 if(request.url!=="/favicon.ico"){ //清除第2此訪問 7 var other =new otherfun (response);//otherfuns.js 里的方法controller被調用 8 //otherfun (response);//otherfuns.js 里的方法controller被調用 9 response.end(); 10 } 11 }).listen(8000); 12 console.log('Server running at http://127.0.0.1:8000/');
我們運行:node 2_callFunction,打開瀏覽器
3. 提供調用多個函數的寫法:
第一種:
1 //支持多個函數 2 function controller(res){ 3 res.write("執行controller <br>"); 4 res.end(); 5 } 6 function call(res){ 7 res.write("執行call"); 8 res.end(); 9 } 10 module.exports.controller = controller; 11 module.exports.call =call;
第二種:
1 //支持多個函數 2 module.exports={ 3 controller:function(res){ 4 res.write("執行controller <br>"); 5 res.end(); 6 }, 7 call:function(res){ 8 res.write("執行call <br>"); 9 res.end(); 10 } 11 }
調用方式相比只支持一個函數的方式,需要將:otherfun (response);
修改成如下調用方式
otherfun.controller(response);//otherfuns.js 里的函數controller被調用
4. 模塊化調用應用(面向對象)
我們建立一個User對象
1 //--------------User.js-------------- 2 function User(id,name,age){ 3 this.id=id;//屬性 4 this.name=name;//屬性 5 this.age=age;//屬性 6 this.enter=function(){//方法 7 console.log("進入圖書館"); 8 } 9 } 10 module.exports = User;
再建一個Teacher對象
1 //-------------------models/Teacher.js--------- 2 var User = require('./User'); 3 function Teacher(id,name,age){ 4 User.apply(this,[id,name,age]);//繼承User 5 this.teach=function(res){//自有方法 6 res.write(this.name+"老師講課"); 7 } 8 } 9 module.exports = Teacher;
Teacher繼承User對象,有id,name,age屬性,除了enter方法外還定義了teach方法。
apply可以執行多次,所以可以繼承多個對象,不如其他語言的面向對象更加嚴格。
在server端可以如下調用teacher。Teacher(1,'李四',30),初始化了一個實例對象
1 //----------------------n3_modalcall.js------------- 2 var http = require('http'); 3 var Teacher = require('./models/Teacher'); 4 http.createServer(function (request, response) { 5 response.writeHead(200, {'Content-Type': 'text/html; charset=utf-8'}); 6 if(request.url!=="/favicon.ico"){ //清除第2此訪問 7 teacher = new Teacher(1,'李四',30); 8 teacher.teach(response); 9 response.end(''); 10 } 11 }).listen(8000); 12 console.log('Server running at http://127.0.0.1:8000/');
我們運行:node 3_modelCall,打開瀏覽器
5. 用函數名的字符串調用
otherfuns.js內容如下
1 //支持多個函數 2 module.exports={ 3 controller:function(res){ 4 res.write("執行controller <br>"); 5 }, 6 call:function(res){ 7 res.write("執行call <br>"); 8 } 9 }
再server里通過字符串調用otherfuns里的函數
1 //-----------------用函數名的字符串調用------------------ 2 var http = require('http'); 3 var otherfun = require("./models/otherfuns.js"); 4 http.createServer(function (request,response) { 5 response.writeHead(200,{'Content-Type': 'text/html; charset=utf-8'}); 6 if(request.url!=="/favicon.ico"){ 7 //-------用字符串調用對應的函數--- 8 funname = 'controller'; 9 otherfun[funname](response); 10 otherfun['call'](response); 11 response.end(); 12 } 13 }).listen(8000); 14 console.log('Server running at http://127.0.0.1:8000/');