exports与module.exports的区别,export与export.defult区别

在 JS 模块化编程中,之前使用的是 require.js 或者 sea.js。随着前端工程化工具 webpack 的推出,使得前端 js 可以使用 CommonJS 模块标准或者使用 ES6 moduel 特性。
在 CommonJs 模块标准中我们载入模块使用的是 require(),输出模块用的是 exports 或者 module.exports
在 ES6 中载入模块我们用的是 import ,输出模块用的是 export

exports 与 module.exports 的区别

//载入模块
var m = require('./moduleA.js')
m.callName()
//输出模块
exports.callName = function(){
    console.log('jesse')
}
//也可以这样输出
module.exports.callName = function(){
    console.log('jesse')
}

module.exports 才是 module 模块的真正接口,而 exports 可以理解为它的一个副本
虽然修改 exports 对象的时候也会修改 module.exports 对象,但最终返回给调用的是 module.exports 对象
当 module.exports 对象通过赋值方式进行设定后,就已经和 exports 对象没关系了

so,我的理解是 exports 输出的只是要输出的对象的某个属性,module.exports 才真正输出的是要输出的对象。
一个模块文件中可以有多个 exports 输出,但只能有一个 module.exports 输出
这种方式普遍用于 node 中模块的编写

export 与 export.defult 区别

es6moduel 特性在 node 环境中并不能完全支持,解决方法是用 babel 编译

//载入模块
import {callName} from './moduleA.js'
callName()
//输出模块
export function callName (){
    console.log('jesse')
}

1.export 与 export default 均可用于导出常量、函数、文件、模块等 2.你可以在其它文件或模块中通过 import+(常量 | 函数 | 文件 | 模块)名的方式,将其导入,以便能够对其进行使用 3.在一个文件或模块中,export、import 可以有多个,export default 仅有一个 4.通过 export 方式导出,在导入时要加{ },export default 则不需要