Trying to do such a thing using RequireJS:
- There is a
Script1.jsscript that performs some actions with data from aDB.jsscript. - There is a
Script2.jsscript that also performs some actions with data from aDBscript and loads this script fromscript1.js. - There is the
DBscript itself, which simply has variables with the necessary data.
Approximate piece of code:
//Script1.js define(["DB", "Script2"], function (DB, Script2) { return function Script1() { console.log(DB); //выводит function () { }, всё круто this.script2 = new Script2(); } ); //Script2.js define(["DB"], function (DB) { return function Script2(cid) { console.log(DB); //выводит undefined, не то, что хотелось бы } }); //DB.js define(function () { //сюда код попадает единственный раз, когда RequireJS подключает его из Script1.js var DB = function () {}; DB.someData = { one: "two" }; return DB; }); For some reason, undefined comes to Script2.js in the DB variable. Those. such as the DB file was already loaded once, and RequireJS decided to just do nothing and not return. I am sure that he knows what he is doing, but I want to know how to get what I ask from him all the same?
DB is such a kind of "class" with static properties. In this implementation, while trying to solve the problem, I realized something that was now written if it worked as I thought, then each time a new DB constructor function would be returned, but I would like a link to the one created at the beginning.
In general, I tried to explain as I could.
Bottom line: comes undefined , although I expected a DB constructor function. If you want to help, but the problems are not understood, I will try to explain in more detail.