使用 Object.assing 将全局对象与另一个对象组合,无法访问连接的对象?

Combining global object with another object using Object.assing, not being able to access the concatinated objects?

我有很多东西要设置到 node.js 中的全局变量中。我试着做一些……

global.awesomeFunc = (num1, num2) => {
    if (num1 == 9 && num2 == 10) return 21;
    return num1 + num2;
}
global.otherCoolFunc = () => {return 27 / 0;}

但是,我意识到我只是通过不断地为每件事声明 global 来占用很多 space。所以我查看了连接并认为我可以将我所有的东西都放在一个对象中,然后将它与全局变量连接起来,如:

let utils = {
    awesomeFunc: (num1, num2) => {/*You get my point...*/},
    otherCoolFunc: () => {return 27 / 0;}
}
global = Object.assign(utils, global);

但是在那之后我不能使用 awesomeFuncotherCoolFunc 而不必在它之前声明 global 吗?通过使用 global.awesomeFunc 我可以 运行 没有 global 的功能,但是使用 concat 我必须使用 global.awesomeFunc... 无论如何我可以重新获得使用 [= 的能力20=] 没有 global 而使用 Object.assign?

Object.assign(target, ...sources)

Parameters

target The target object.

sources The source object(s).

Return value

The target object.

在执行 global = Object.assign(utils, global); 时,您将在 global 对象中获得具有 global 属性的 utils

反转 sourcetarget :

global = Object.assign(global, utils); 所以你保留旧的全局属性 utils