如何'inject' 各种js函数的通用代码?

How do I 'inject' common code to various js functions?

我正在构建一个测验系统和问题,在 JS 中,有通用代码(第二行到 //more stuff):

 let qId = 1000003, // question ID number, unique to this question
        uId = QWIZM.state.uId,
        sd = QWIZM.methods.toSigDigs,
        stringify = QWIZM.methods.stringify,
        sin = utils.sin,
        cos = utils.cos,
        asin = utils.asin,
        acos = utils.acos,
        tan = utils.tan,
        atan = utils.atan,
        thisQuiz = QWIZM.state.thisQuiz,
        ov = QWIZM.methods.overlayVariable,
        seed = qId > uId ? qId % uId : uId === qId ? uId : uId % qId,
        lcrng = new utils.LCRNG(seed);

// more stuff here

如何插入通用代码,使我的文件看起来像这样:

let qId = 1000003, // question ID number, unique to this question
commonToAll();
// more stuff here

当然也不一定是函数...

您可以使用 ES6 模块来实现。 Mozilla 对 MDN 有相当全面的解释: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules