Difference between export const foo, export default foo and module.exports = foo

后端 未结 3 872
感动是毒
感动是毒 2021-02-01 15:57

I am really confused about:

  1. export const foo
  2. export default foo
  3. module.exports = foo;

I kno

3条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-02-01 16:41

    export const foo : exports constants (ES6) export default foo : exports object (ES6)

    Above statements are ECMA Script 2015 (aka ES6) implementation.

    In an normal ES6 JS file one can export any object(variable) or constants. Pls note that you cannot change constant reference, though internal structure can be modified(weird).

    In ES6 one can have multiple exports in module(script file). which can be added in calling script as

    import {Obj1, Obj2} from module_file
    

    coming to export default, One can have only one export default in module. and while importing when exact names are not defined default is been picked up.

    module.exports = foo; is older implementation and it is same as export default. except it is imported with require statement instead of import

    for more refer https://developer.mozilla.org/en/docs/web/javascript/reference/statements/export

提交回复
热议问题