zoukankan      html  css  js  c++  java
  • Node.js 基础--01 函数的调用

    这里讲的是调用函数和函数本身不在一个文件内的情况;

    1,单个函数

    引入文件和调用文件时的写法

    var http=require("http");
    var otherFunc=require('./js/other.js');
    http.createServer(function(request,response){
        response.writeHead("200",{"Content-Type":"text/html;charset=utf-8"});
        if(request.url!=="/favicon.ico"){
            otherFunc(response);
            response.write("Hello World");
            response.end('hello,世界');
        }
    }).listen(8000);
    console.log("Server running at http:127.0.0.1:8000");
    
    function func1(){
        console.log("func1被执行");
    }
    单个函数的引入

    js文件本身的写法:

    function func2(res){
        res.write("你好,我是fun2");      
    }
    module.exports=func2;//必须写这行代码,necessary;且只支持导出一个函数

    2,多个函数

    调用的时候,要在引用文件后面加上具体哪个函数名,跟面向对象的写法差不多;

    var otherFunc= require('./js/other.js');

    otherFunc.func2(response);//调用;;也可以写成 otherFunc[func2](response);这样,func2就可以用变量代替了;;点语法可以用[]代替,js那里讲过,函数名需要是字符串形式。。。(这个报错查了好半天,,丢yin啊,,)

    js文件本身的写法:

    module.exports={
        func2:function(res){
            res.write("你好,我是fun2");      
        },
        func3:function(res){
            res.write("你好,我是fun3");      
        }
    }
    被引入的文件-多个js

    Over...

  • 相关阅读:
    Computer Browser服务自动停止
    Group By中Case分类统计
    C#判断网络状态
    Win7中VC6打开文件报错(转)
    SqlBulkCopy(大数据量拷贝)
    vc6开发ActiveX并发布全攻略(二)(转)
    VC6 Activex控件调试
    VC MessageBox
    常用基本AT指令
    WinForm自动重启
  • 原文地址:https://www.cnblogs.com/new-dream-new-hope/p/7078458.html
Copyright © 2011-2022 走看看