browserify - Javascript environment sharing between different script tags in browser -
i new javascript , coming c++ background finding myself getting little confused. have javascript file setting working enviornment factory class javascript file so:
<body> <script src = "environment.js" ></script> <script src = "main.js" ></script> </body> the idea different index.html have different environment.js file configure environment differently share same main.js file.
i have simple factory.js file gets required both environment.js , main.js (this environment.js configures , main.js uses)
factory.js
function platformfactory(){ this._factories = {}; } platformfactory.prototype.registerservice = function(name, service) { this._factories[name] = service; } platformfactory.prototype.requestservice = function(name) { if (name in this._factories) { return this._factories[name]; } throw error("no service called " + name + " found in platform factory"); } var factory = new platformfactory(); module.exports = factory; environment.js
(function () { var mod_platform_factory = require("../../../platform/factory.js"); mod_platform_factory.registerservice("connection", require("./connection.js")); })(); i have stripped both 2 files ease of reading , ommited including connection.js code believe unecessary. works expected , connection object sits happily in _factories of singular platformfactory object.
then starts executing main.js
var mod_platform_factory = require("../../libs/platform/factory.js"); $(document).ready(function () { var connection = mod_platform_factory.requestservice("connection"); connection.connect(); } ) now never line connection.connect() because requestservice function throws error places inside platformfactory.requestservice. looking platformfactory._factories object there no service called connection.
can please explain doing wrong please?
thanks
the mod_platform_factory created in environment.js overwritten mod_platform_factory create in main.js.
the latter 1 not have connection.
Comments
Post a Comment