Js模块中的Varibale不会更改其值

时间:2019-09-11 08:35:08

标签: javascript node.js module

我正在尝试创建DBConnection类的单例实例, 在DBConnection.js中,我有以下代码:

class DBConnection {
    constructor(url) {}
}

let dbInstance = null;

module.exports = (url) => {
if(dbInstance) {
return dbInstance; }

else if(url) {

dbInstance = new DBConnection(url);
return dbInstance;
}
else {
  return null;
} 

在App.js文件中,我有以下代码:

const init = (url) => {

require('./DBConnection')(url) 

}

init('some_conn_url);

setTimeout(() => {

const conn = require('./DBConnection')(); //conn always has the null value?! 
}, 5000)

似乎dbInstance不会更改其值?为什么?

0 个答案:

没有答案