NODEJS中的exports和module.exports

1 exports用法

function func1(){
	return "world";
}

exports.hellos = func1;   //exports.后面才是向外面暴露的名字,
						 //在外面需要使用hellos调用函数,func1是无效的

exports.name = "mary";
exports.age = "19";

exports.hellos2 = function(){  //匿名函数
	console.log("hello");
}
  • require("./test.js") 或者require("./test")括号里面写js文件的名称,后缀有没有都可以。
  • 另外:exports. 后面才是向外面暴露的,上面代码的真正函数名func1在外面是无效的

测试:
在这里插入图片描述

2 module.exports的用法

把上面代码中的exports前面全部加上module.

function func1(){
	return "world";
}

module.exports.hellos = func1;   //exports.后面才是向外面暴露的名字,
						 //在外面需要使用hellos调用函数,func1是无效的

module.exports.name = "mary";
module.exports.age = "19";

module.exports.hellos2 = function(){  
	console.log("hello");
}

同样是可以的。

再改成下面这样也是可以的:

module.exports = {
	func1:function(){
		return "hello";
	},
	name:"mary",
	age:10
}

所以,对于module.exports,

  • 既可以使用module.exports.xxx = yyy 的形式
  • 也可以使用module.exports = {},直接赋值一个对象,{}花括号是一个对象的定义
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值