温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

nodeJS调用函数

发布时间:2020-08-15 07:58:08 来源:网络 阅读:935 作者:yeleven 栏目:开发技术

一、调用普通函数

声明函数:

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



向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI