新聞中心
這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷解決方案
Node.js函數(shù)的示例分析-創(chuàng)新互聯(lián)
這篇文章將為大家詳細(xì)講解有關(guān)Node.js函數(shù)的示例分析,小編覺(jué)得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。
內(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ù)器工作的
帶著這些知識(shí),我們?cè)賮?lái)看看我們簡(jiǎn)約而不簡(jiǎn)單的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ù)傳遞了一個(gè)匿名函數(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);
關(guān)于“Node.js函數(shù)的示例分析”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,使各位可以學(xué)到更多知識(shí),如果覺(jué)得文章不錯(cuò),請(qǐng)把它分享出去讓更多的人看到。
本文題目:Node.js函數(shù)的示例分析-創(chuàng)新互聯(lián)
分享網(wǎng)址:http://www.dlmjj.cn/article/dippij.html