簡體   English   中英

帶默認參數的模塊導出

[英]Module export with default parameters

我正在嘗試在 Node.js 中導出一個模塊,其中包含一些功能。 其中一個函數具有可選參數,其中包括默認值。

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

這些功能在未導出時起作用。 移入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)

我知道參數可以在 function 的代碼中進行默認設置。 不過,括號中的默認參數在module.exports中不起作用嗎? 任何幫助修復它? 謝謝。

您的 object 表示法不正確:

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