1、函数的概念
一个函数可以作为另一个函数的参数,
可以先定义一个函数,然后传递,也可以在传递参数的地方直接定义函数
2、回调函数,先定义后传递
function say(word){
console.log(word);
}
function execute(someFunction,value){
someFunction(value);
}
execute(say,"Hello");
3、匿名函数(如果函数不需要复用的话,就可以使用匿名函数的形式)
function execute(someFunction,value){
someFunction(value);
}
execute(function (word){
console.log(word);
},"Hello");
4、HTTP服务器端的函数传递
//引入node提供的原生API---http模块
var http = require("http");
//使用http来创建服务
http.createServer(function(req,res){
//定义一个返回的HTTP头,返回状态为200时,返回头信息
res.writeHead(200,{'Content-Type':'text/plan'});
//发送响应数据(Hello World)
res.end('Hello World!\n');
//使用级联函数对监听端口进行设定
}).listen(8000);
//如何证实服务已经运行成功——服务运行后输出一行信息
console.log("server is running...");
网友评论