require()和exports

1、用require()引入外部模块

  • require可以传递一个文件的路径作为参数,node将会自动根据该路径来引入外部模块
    ps.这里路径,如果使用相对路径,必须以.或…开头

2、用exports暴露内部数据或方法

  • 只需要将需要暴露给外部的变量或方法设置为exports的属性即可

03.module

var md = require("./02.module");
console.log(md)

02.module

console.log("我是一个模块,我是02.module.js");

exports.x = "我是02.module.js中的x";
exports.y = "我是y";
exports.fn = function () {

};

结果

在这里插入图片描述

3、exports和module.exports

  • exports:只能使用.的方式来向外暴露内部变量
    exports.xxx = xxx
  • module.exports既可以通过.的形式,也可以直接赋值
    module.exports.xxx = xxxx
    module.exports = {}
expoerts.name = "猪八戒",
expoerts.age = 28,
expoerts.sayName = function () {
		console.log("我是猪八戒");
}

nodule.exports  = {
	name:"猪八戒",
	age:28,
	sayName:function () {
		console.log("我是猪八戒");
	}
  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值