Node.js 基础--01 函数的调用

这里讲的是调用函数和函数自己不在一个文件内的状况;html

1,单个函数ide

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

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文件自己的写法:ui

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

2,多个函数url

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

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

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

js文件自己的写法:htm

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

 

 

Over...对象

相关文章
相关标签/搜索