一、调用普通函数
声明函数:
function fun1(res) {
console.log("fun1");
res.write("I'm fun1");
}
在同一文件内调用:
fun1(response);
二、调用其它文件中的函数
声明函数并导出:
function fun2(res) {
console.log('我是fun2');
res.write('I'm fun2');
}
module.exports = fun2;
引入模块:
var otherfun = require('./models/otherfuns');
'./models/otherfuns.js' 表示fun2的所在文件路径
调用函数:
otherfun.fun2(response);
三、导出多个函数
module.exports = {
fun2:function(res){
console.log('我是fun2');
res.write('你好,我是fun2');
},
fun3:function(res){
console.log('我是fun3');
res.write('你好,我是fun3');
}
}
四、用字符串调用对应的函数
otherfun['fun2'](response);
otherfun['fun3'](response);
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。