笔记nodejs:module.exports和exports

nodejs模块-module.exports和exports

导读:一个js文件即为一个模块,用下列两个暴露,用require引入。
1.暴露对象

module.exports = {
  // 暴露对象
}
module.exports = function() {
  // ...
}
module.exports = Hello;
function hello(){};

2.暴露方法或属性

exports.world = world;
exports.world = function(){

}

如果要对外暴露属性或方法,就用 exports 就行
要暴露对象(类似class,包含了很多属性和方法),就用 module.exports。

不建议同时使用 exports 和 module.exports.否则会使得 exports 上暴露的属性或者方法失效。原因在于,exports 仅仅是 module.exports 的一个引用

猜你喜欢

转载自blog.csdn.net/m0_49888984/article/details/108006457
今日推荐