这里讲的是调用函数和函数自己不在一个文件内的状况;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"); } }
Over...对象