node.js函数和函数调用

1.node.js函数

    Node.js中函数的使用与Javascript类似,举例来说,你可以这样做

    1.不带参的函数

function sayhello(){
    console.log('Hello World');
}
sayhello()
//运行结果  Hello World

    2.带参的函数

function sayyouwrite(youwrite){
    console.log(youwrite);
}
sayyouwrite('你好')
//运行结果  你好
    3.多个参数函数
function sayyouwrite2(youwrite1,youwrite2,youwrite3){
    console.log(youwrite1+youwrite2+youwrite3);
    console.log(youwrite1);
    console.log(youwrite2);
    console.log(youwrite3);
}
sayyouwrite('你好')
// 运行结果
// 你好!世界!中国!
// 你好!
// 世界!
// 中国!

    4.匿名函数

function execute(someFunc, value) {
    someFunc(value)
}

execute(function (world) {
    console.log(world)
}, "Hello world")

2.函数的调用

    一个http经典小例子

    1.js文件内部函数调用

var http = require('http')
http.createServer(function (request, response) {
    // 发送 HTTP 头部
    // HTTP 状态值: 200 : OK
    // 内容类型: text/plain
    response.writeHead(200, {'Content-Type': 'text/html;charset=utf-8'});
    if(request.url="/favicon.ico"){
        fun1(response);
        response.end('')
    }
}).listen(8888);
function fun1(res) {
    console.log("我是fun1")
    res.write("你好,我是fun1|")
}
// 终端打印如下信息
console.log('Server running at http://127.0.0.1:8888/');

     运行结果:


    2.调用其他js文件内的函数

   m1.js文件内容

var http = require('http')
var fun2 = require("./m2.js")
http.createServer(function (request, response) {
    // 发送 HTTP 头部
    // HTTP 状态值: 200 : OK
    // 内容类型: text/plain
    response.writeHead(200, {'Content-Type': 'text/html;charset=utf-8'});
    if(request.url="/favicon.ico"){
        fun1(response);
        fun2(response);
        response.end('')
    }
}).listen(8888);
function fun1(res) {
    console.log("我是fun1")
    res.write("你好,我是fun1|")
}
// 终端打印如下信息
console.log('Server running at http://127.0.0.1:8888/');

    ps:m2.js与m1.js在同一目录下这里用的是相对路径调取

    m2.js文件内容

function fun2(res) {
    console.log("我是fun2")
    res.write("你好,我是fun2")

}
 module.exports = fun2;//只能引用一个函数

   运行结果:


 在这个例子中我们成功调用了m2.js文件中fun2函数,我们使用的是module.experts = 的方法,这个方法有一 个弊端:只能调用一个函数,如果我们想调用多个函数如下

  3.调用其他js文件中多个函数

    我们m1.js文件修改为

var http = require('http')
var funx = require("./m2.js")
http.createServer(function (request, response) {
    // 发送 HTTP 头部
    // HTTP 状态值: 200 : OK
    // 内容类型: text/plain
    response.writeHead(200, {'Content-Type': 'text/html;charset=utf-8'});
    if(request.url="/favicon.ico"){
        fun1(response);
        funx.fun2(response);
        funx.fun3(response);
        response.end('')
    }
}).listen(8888);
function fun1(res) {
    console.log("我是fun1")
    res.write("你好,我是fun1|")
}
// 终端打印如下信息
console.log('Server running at http://127.0.0.1:8888/');

    我们将m2.js文件修改为

module.exports ={
    fun2:function (res) {
        console.log("我是fun2")
        res.write("你好,我是fun2|")
    },
    fun3:function (res) {
        console.log("我是fun3")
        res.write("你好,我是fun3")
    }
}

    运行结果:


    这一次我们将m2.js文件里面我需要调用的函数以:方法名:匿名函数 的范式写进了module.exports = {}里 面即可一次在m1文件里面调用多个m2文件里面的函数!

    同时我们也可以将m1.js文件里面的

        funx.fun2(response);
        funx.fun3(response);

    替换为

        funx['fun2'](response);
        funx['fun3'](response);

    或

        fname2 = 'fun2';
        fname3 = 'fun3';
        funx[fname2](response);
        funx[fname3](response);

    也可以进行m2.js里面的函数调用,下面两种方式可以进行灵活调用

    


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值