简体   繁体   English

将分配变量解构为全局常量

[英]Destructuring Assignment Variables as Global Constants

I have bunch of functions declared across my modules, which I would like to use as global constants for all my codebase. 我在模块中声明了一堆函数,我想将其用作所有代码库的全局常量。

Now these functions declared as local constants: 现在,这些函数声明为局部常量:

const { function1, function2, function3, ..., functionN } = require('./my-module-1')
const { functionA, functionB, functionC, ..., functionN } = require('./my-module-2')

Is there shorter way to convert these function to globals instead of: 有没有更短的方法将这些函数转换为全局变量,而不是:

function1 = require('./my-module-1').function1
function2 = require('./my-module-1').function2
function3 = require('./my-module-1').function3
functionN = require('./my-module-1').functionN

functionA = require('./my-module-2').functionA
functionB = require('./my-module-2').functionB
functionC = require('./my-module-2').functionC
functionN = require('./my-module-2').functionN

I've tried just removing const keyword as well as added brackets around the destructuring assignment but both caused syntax error 我试图删除const关键字以及在解构赋值周围添加方括号,但两者均导致语法错误

{ function1, function2, function3, ..., functionN } = require('./my-module-1')
{ functionA, functionB, functionC, ..., functionN } = require('./my-module-2')

({ function1, function2, function3, ..., functionN } = require('./my-module-1'))
({ functionA, functionB, functionC, ..., functionN } = require('./my-module-2'))

My suggestion, 我的建议,

global.modules = require('sample-package'); // get all modules and set it to global object

let { module1 } = global.modules; // where you want use this

let { module2 } = global.modules; // where you want use this

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

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