內(nèi)容:普通函數(shù),匿名函數(shù),函數(shù)傳遞是如何讓HTTP服務(wù)器工作的
###普通函數(shù)
例子:
function say(word) { console.log(word);}function execute(someFunction, value) { someFunction(value);}execute(say, "Hello");###匿名函數(shù)function execute(someFunction, value) { someFunction(value);}execute(function(word){ console.log(word) }, "Hello");####################################################################################
函數(shù)傳遞是如何讓HTTP服務(wù)器工作的
帶著這些知識,我們再來看看我們簡約而不簡單的HTTP服務(wù)器:
var http = require("http");http.createServer(function(request, response) { response.writeHead(200, {"Content-Type": "text/plain"}); response.write("Hello World"); response.end();}).listen(8888);現(xiàn)在它看上去應(yīng)該清晰了很多:我們向 createServer 函數(shù)傳遞了一個匿名函數(shù)。
用這樣的代碼也可以達(dá)到同樣的目的:
var http = require("http");function onRequest(request, response) { response.writeHead(200, {"Content-Type": "text/plain"}); response.write("Hello World"); response.end();}http.createServer(onRequest).listen(8888);總結(jié)
以上所述是小編給大家介紹的Node.js 函數(shù),希望對大家有所幫助,如果大家有任何疑問歡迎給我留言,小編會及時回復(fù)大家的!
新聞熱點(diǎn)
疑難解答
圖片精選