简体   繁体   English

带默认参数的模块导出

[英]Module export with default parameters

I am trying to export a module in Node.js with some functions in it.我正在尝试在 Node.js 中导出一个模块,其中包含一些功能。 One of the functions have optional parameters which include default values.其中一个函数具有可选参数,其中包括默认值。

module.exports = {
    foo = (a, b = 2, c = {y:0}) => {
        // Code
    },
    bar = () => { 
        // Codes 
    },
}

The functions work when it is not exported.这些功能在未导出时起作用。 When moved into module.exports , the following error occurs:移入module.exports时,会出现以下错误:

SyntaxError: Invalid shorthand property initializer
    at Module._compile (internal/modules/cjs/loader.js:723:23)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
    at Module.load (internal/modules/cjs/loader.js:653:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
    at Function.Module._load (internal/modules/cjs/loader.js:585:3)
    at Module.require (internal/modules/cjs/loader.js:692:17)
    at require (internal/modules/cjs/helpers.js:25:18)
    at Object.<anonymous> (/<link to this file>:2:62)
    at Module._compile (internal/modules/cjs/loader.js:778:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
    at Module.load (internal/modules/cjs/loader.js:653:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
    at Function.Module._load (internal/modules/cjs/loader.js:585:3)
    at Module.require (internal/modules/cjs/loader.js:692:17)
    at require (internal/modules/cjs/helpers.js:25:18)
    at Object.<anonymous> (/<link to server.js file>:20:18)

I am aware the parameters can be default-ed in the code of the function.我知道参数可以在 function 的代码中进行默认设置。 Still, does default-ing parameters in brackets not work in module.exports ?不过,括号中的默认参数在module.exports中不起作用吗? Any help in fixing it?任何帮助修复它? Thanks.谢谢。

You object notation is incorrect:您的 object 表示法不正确:

Change foo = () => {} to foo: () => {}foo = () => {}更改为foo: () => {}

module.exports = {
    foo: (a, b = 2, c = {y:0}) => {
        // Code
    },
    bar: () => { 
        // Codes 
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM