Node.js(二):模塊&函數(shù)

一、同步/異步&阻塞/非阻塞

(參考鏈接:https://www.zhihu.com/question/19732473/answer/20851256,來(lái)源知乎,作者嚴(yán)肅)
1、同步與異步同步和異步關(guān)注的是消息通信機(jī)制。
所謂同步,就是在發(fā)出一個(gè)調(diào)用時(shí),由調(diào)用者主動(dòng)等待這個(gè)調(diào)用的結(jié)果。而異步則是相反,調(diào)用在發(fā)出之后,調(diào)用者不會(huì)立刻得到結(jié)果。而是在調(diào)用發(fā)出后,被調(diào)用者通過(guò)狀態(tài)、通知來(lái)通知調(diào)用者,或通過(guò)回調(diào)函數(shù)處理這個(gè)調(diào)用。
(同步異步是針對(duì)被調(diào)用者)
2、和非阻塞關(guān)注的是程序在等待調(diào)用結(jié)果的狀態(tài)。
阻塞調(diào)用是指調(diào)用結(jié)果返回之前,當(dāng)前線程會(huì)被掛起。調(diào)用線程只有在得到結(jié)果之后才會(huì)返回。
非阻塞調(diào)用指在不能立刻得到結(jié)果之前,該調(diào)用不會(huì)阻塞當(dāng)前線程。

1.1、同步

var fs = require('fs');

fs.unlinkSync('/tmp/shiyanlou'); // 函數(shù)名最后的Sync 表示是同步方法
console.log('成功刪除了 /tmp/shiyanlou');

1.2、異步

var fs = require('fs'); // 載入fs模塊

fs.unlink('/tmp/shiyanlou', function(err) {
    if (err) {
        throw err;
    }
    console.log('成功刪除了 /tmp/shiyanlou');
});

異步方法中回調(diào)函數(shù)的第一個(gè)參數(shù)總是留給異常參數(shù)(exception),如果方法成功完成,那么這個(gè)參數(shù)為null或者undefined。

二、函數(shù)

//-----------------用函數(shù)名的字符串調(diào)用------------------
var        http        =        require('http');    
var  otherfun  =  require("./models/otherfuns.js");
http.createServer(function        (request,        response)        {        
                response.writeHead(200,        {'Content-Type':        'text/html;        charset=utf-8'});        
        if(request.url!=="/favicon.ico"){        //清除第2此訪問(wèn)
          //fun1(response);
          //-------用字符串調(diào)用對(duì)應(yīng)的函數(shù)---
          funname  =  'fun2;
          otherfun[funname](response);

          //otherfun['fun3'](response);
          response.end('');    
    }
}).listen(8000);        
console.log('Server  running  at  http://127.0.0.1:8000/');
//-------------------models/otherfuns.js--------------------------      
function  controller(req,res){      
    //res.write("發(fā)送");      
    call('hello',req,res);      
    res.end("");      
}      
function  call(res){      
    console.log('call');      
}      
//支持多個(gè)函數(shù)      
module.exports={      
    fun2:function(res){      
    res.write("this is fun2");
    }      ,
    fun3:function(res){      
    res.write("this is fun3");
    }      
}      

總結(jié):
1、通常使用module.exports={ , ,};來(lái)申明多個(gè)函數(shù)
2、調(diào)用其他js文件里面的函數(shù)時(shí),需要用require先引入,然后采用funname方式來(lái)調(diào)用,更加靈活。

三、模塊的調(diào)用

/----------------------n3_modalcall.js-------------  
var  http =  require('http');    
//var  User  =  require('./models/User');
var  Teacher  =  require('./models/Teacher');
http.createServer(function        (request, response)        {        
                response.writeHead(200,  {'Content-Type':  'text/html;        charset=utf-8'});        
        if(request.url!=="/favicon.ico"){   //清除第2此訪問(wèn)
          teacher  =  new  Teacher(1,'李四',30);
          teacher.teach(response);
          response.end('');    
    }
}).listen(8000);        
console.log('Server running at http://127.0.0.1:8000/');
//--------------User.js--------------  
function  User(id,name,age){
    this.id=id;
    this.name=name;
    this.age=age;
    this.enter=function(){
        console.log("進(jìn)入圖書館");
    }
}
module.exports    =    User;
//-------------------models/Teacher.js---------  
var  User  =  require('./User');
function  Teacher(id,name,age){
    User.apply(this,[id,name,age]);
    this.teach=function(res){
        res.write(this.name+"老師講課");
    }
}
module.exports    =    Teacher;

總結(jié):
1、function相當(dāng)于java的類,寫完要用module.exports申明一下,在其他的類中才能用require("路徑")來(lái)引入,實(shí)例化對(duì)象之后即可使用。
2、子類繼承父類的方法時(shí)apply方法,如User.apply(this,[id,name,age]);

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容