我在我的模块中声明了一堆函数,我想将它们用作我所有代码库的全局常量。
现在,这些函数声明为局部常量:
const { function1, function2, function3, ..., functionN } = require('./my-module-1')
const { functionA, functionB, functionC, ..., functionN } = require('./my-module-2')
有没有更短的方法将这些函数转换为全局变量而不是:
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
我尝试只删除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'))
我的建议,
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