我正在尝试创建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不会更改其值?为什么?