gpt4 book ai didi

javascript - NodeJS循环模块应用程序结构

转载 作者:太空宇宙 更新时间:2023-11-04 01:47:56 25 4
gpt4 key购买 nike

我有 3 个文件,其结构如下:

  1. root.js
  2. mod1.js
  3. mod2.js

mod1.js 和 mod2.js 都是必需的/在 root.js 中实例化,并且 mod1.js 和 mod2.js require("root.js");这样我就可以对 root.js 中的公共(public)函数执行回调...我遇到的问题是 const root = require("root.js");在 mod1.js 和 mod2.js 中都是 {} 或空对象。特别是当我在 mod1 和 mod2 中放入更多代码时,它们最终都会变成 {}

错误屏幕截图可以在这里看到: https://github.com/DarceyLloyd/NodeAppArchitecture/blob/master/issue.png

所以问题是,什么结构或什么代码在实例化过程中正确地实现了每个类/函数/对象的返回?结构性变化?我已经使用映射到正文的 keyup 函数对此进行了测试,以便当我按下 space eit 时需要 root.js,然后运行其 getA 函数就可以了,但是它仅在按键时执行此操作,因此不在类/函数/对象实例化/创建期间执行此操作。有想法吗?

所有文件都可以在这里查看: https://github.com/DarceyLloyd/NodeAppArchitecture

以及 stackoverflow 引用:

root.js

````var 根 = 函数(){ 这个.a = -1; this.b = -1;

const mod1 = require("./mod1.js"); // Runs 2nd?
const mod2 = require("./mod2.js"); // Runs 1st?

function init(){
this.a = 0;
this.b = 0;
}

this.incA = function() { this.a++; }
this.incB = function() { this.a++; }
this.getA = function() { return this.a; console.log(a); }
this.getB = function() { return this.b; console.log(b); }

init();

}

//缓存输出,因此 new 只会被调用一次module.exports = new Root();````

mod1.js

````var Mod1 = 函数(){ const root = require("./root.js");

function init(){
console.log("Mod1()");
console.log(root); // result is {}
//root.incA(); // error incA doesn't exist on object root
}

init();

}

//缓存输出,因此 new 只会被调用一次module.exports = new Mod1();````

mod2.js

````var Mod2 = 函数(){ const root = require("./root.js");

function init(){
console.log("Mod2()");
console.log(root); // result is {}
//root.incB(); // error incB doesn't exist on object root
}

init();

}

//缓存输出,因此 new 只会被调用一次module.exports = new Mod2();````

最佳答案

很抱歉让您失望,但只要您在应用程序中进行循环导入,您就无法获得正确的“实例化”。为了实现正确的行为,您不应该以循环方式导入。当您实例化一个对象时,另一个对象尚未实例化。我建议您采用以下解决方案,以确保您尝试访问的对象已实例化。

        // ROOT 
const mod1 = require("./mod1.js"); // Runs 2nd?
const mod2 = require("./mod2.js"); // Runs 1st?

var Root = function(){
this.a = -1;
this.b = -1;

this.init = function(){
this.a = 0;
this.b = 0;
}

this.incA = function() { this.a++; }
this.incB = function() { this.a++; }
this.getA = function() { return this.a; console.log(a); }
this.getB = function() { return this.b; console.log(b); }
}


// Cached output so new only ever gets called once
module.exports = new Root();

mod2.init()
mod1.init()


// MOD2
var Mod2 = function(){

this.init = function() {
const root = require("./root.js");
console.log("Mod2()");
root.init()
console.log(root); // result is {}
root.incB(); // error incB doesn't exist on object root
}
}

// Cached output so new only ever gets called once
module.exports = new Mod2;


// Mod1
var Mod1 = function(){
this.init = function() {
const root = require("./root.js");
root.init()
console.log("Mod1()");
console.log(root); // result is {}
root.incA(); // error incA doesn't exist on object root
}
}

// Cached output so new only ever gets called once
module.exports = new Mod1;

关于javascript - NodeJS循环模块应用程序结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50776765/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com