webpack 模块中的功能范围

scope of functions in webpack modules

我在 webpack 模块中有一些函数,但它们不能相互调用?如何在 webpack 模块中导入函数 A 以供在同一 webpack 模块中的函数 B 中使用?

例如:

module.exports = {
 handlerror: function(msg) {
    alert(msg)
 }


 init_session: function(key, session_id) {
        var session = init(key,session_id)
        if (session == "fail") { handlerror("failed") }
        return session;
    }
}

在这种情况下,运行时会抱怨 handlerror

先声明函数本身,这样你就可以在代码主体中引用它的独立名称,并将其单独放入module.exports:

function handlerror(msg) {
  alert(msg)
}
module.exports = {
  handlerror
  // other exports
};

// reference handlerror as needed here

请注意,您可以考虑将名称更改为 handleError(或类似名称)以更正拼写并使其更易读。