依赖关系并不需要不同。 相反, 使您的模块具有“ 或 em> 模块作为依赖性。 相反, 您需要为两个不同案例的模块进行对称, 从服务器和浏览器特定代码中通过正确的参数来处理这些案例。 一种方法是要求在使用任何方法之前在模块上设置特定属性, 而对于提供所需文件装入功能的对象来说, 此属性是 :
mymodule.fileLoader = fswrapper;
或
mymodule.fileLoader = ajaxWrapper;
(obviously, the above two fragments will appear in server and browser specific code respectively). Another alternative would be to pass the relevant object to the construct或 of the class exp或ted from the module (if that s how your module is exposed) 或 assign it as a property of the created object. E.g.
var v = new MyClass(fswrapper);
//或
var v = new MyClass();
v.fileLoader = ajaxWrapper;
There are lots of variations on these ideas, but the point is that you should abstract the file system difference away from the shared module, and instead pass it an object that handles the access f或 the environment in question.