代码之家  ›  专栏  ›  技术社区  ›  Danii

使用object.assing将全局对象与另一个对象组合在一起,无法访问具体对象?

  •  0
  • Danii  · 技术社区  · 6 年前

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

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

    但是,我意识到只要不断地声明 global 每件事。所以我查了一下concatenation,我想我可以把所有的东西都设置在一个对象中,然后用全局变量con cat它,比如:

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

    但在那之后我就不能用了 awesomeFunc otherCoolFunc 不用申报 全球的 在它之前?通过使用 global.awesomeFunc 我可以在没有 全球的 但是我必须使用Concat 全球.awsomefunc …我能重新获得使用的能力吗 awsomeFunc 没有 全球的 使用时 Object.assign ?

    1 回复  |  直到 6 年前
        1
  •  1
  •   Taki    6 年前

    global = Object.assign(utils, global); utils global

    source target

    global = Object.assign(global, utils);