资讯专栏INFORMATION COLUMN

node函数使用

番茄西红柿 / 2334人阅读

摘要:在中,一个函数可以作为另一个函数的参数。我们可以先定义一个函数,然后传递,也可以在传递参数的地方直接定义函数。中函数的使用与类似。以上代码中,我们把函数作为函数的第一个变量进行了传递。

在JavaScript中,一个函数可以作为另一个函数的参数。我们可以先定义一个函数,然后传递,也可以在传递参数的地方直接定义函数。

Node.js中函数的使用与Javascript类似。


function say(word) {
        console.log(word);
    }
    function execute(someFunction, value) {
        someFunction(value);
    }
    execute(say, "Hello");

以上代码中,我们把 say 函数作为execute函数的第一个变量进行了传递。这里传递的不是 say 的返回值,而是 say 本身!
这样一来, say 就变成了execute 中的本地变量 someFunction ,execute可以通过调用 someFunction() (带括号的形式)来使用 say 函数。
当然,因为 say 有一个变量, execute 在调用 someFunction 时可以传递这样一个变量。

node.js函数

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")

函数的调用

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文件内的函数

    

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/");
        m2.js:
        function fun2(res) {
            console.log("我是fun2")
            res.write("你好,我是fun2")
        }
        module.exports = fun2;//只能引用一个函数
    
3.调用其他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.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")
            }
        }
    

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

        

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

    替换为

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

    或

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

函数传递是如何让HTTP服务器工作的


var http = require("http");
    http.createServer(function(request, response) {
        response.writeHead(200, {"Content-Type": "text/plain"});
        response.write("Hello World");
        response.end();
    }).listen(8888);
    
    等同于
    
    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);

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/104830.html

相关文章

  • BlockingQueue与Condition原理解析

    摘要:最后一直调用函数判断节点是否被转移到队列上,也就是中等待获取锁的队列。这样的话,函数中调用函数就会返回,导致函数进入最后一步重新获取锁的状态。函数其实就做了一件事情,就是不断尝试调用函数,将队首的一个节点转移到队列中,直到转移成功。  我在前段时间写了一篇关于AQS源码解析的文章AbstractQueuedSynchronizer超详细原理解析,在文章里边我说JUC包中的大部分多线程相...

    TalkingData 评论0 收藏0
  • Node.js 中引入模块:你所需要知道的一切都在这里

    摘要:全局范围生效,不需要。解析本地路径首先来为你介绍对象,可以先在控制台中看一下每一个模块都有属性来唯一标示它。通常是文件的完整路径,但是在控制台中一般显示成。 showImg(https://segmentfault.com/img/remote/1460000009060869?w=1794&h=648); 本文作者:Jacob Beltran 编译:胡子大哈 翻译原文:http:...

    aristark 评论0 收藏0
  • [译]你并不知道Node

    摘要:问题什么是调用栈并且它是的一部分么调用栈当然是的一部分。为什么理解是重要的因为你在每个进程中只能获取一个调用栈。它是一个从事件队列中跳去事件的循环并且将它们的回调压入到调用栈中。当调用栈为空的时候,事件循环可以决定下一步执行哪一个。 你并不知道Node 原文:You don’t know Node 译者:neal1991 welcome to star my articles-tra...

    miqt 评论0 收藏0
  • 什么是Node.js

    Node.js从2009年诞生至今,已经发展了两年有余,其成长的速度有目共睹。从在github的访问量超过Rails,到去年底Node.jsS创始人Ryan Dalh加盟Joyent获得企业资助,再到今年发布Windows移植版本,Node.js的前景获得了技术社区的肯定。InfoQ一直在关注Node.js的发展,在今年的两次Qcon大会(北京站和杭州站)都有专门的讲座。为了更好地促进Node.j...

    CrazyCodes 评论0 收藏0
  • Express 实战(一):概览

    摘要:一个标准性的事件就是年的横空出世。引擎快速处理能力和异步编程风格,让开发者从多线程中解脱了出来。其次,通过异步编程范式将其高并发的能力发挥的淋漓尽致。它也仅仅是一个处理请求并作出响应的函数,并无任何特殊之处。 showImg(https://segmentfault.com/img/remote/1460000010819116); 在正式学习 Express 内容之前,我们有必要从大...

    zhaochunqi 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<