简体   繁体   English

浏览器可以设置全局模块吗?

[英]Can browserify set global modules?

I'm putting together a Backbone Marionette application with Browserify. 我正在将Backbone Marionette应用程序与Browserify组合在一起。 When I want to use Backbone or Marionette related functions, the top of each individual module file looks like this: 当我想使用Backbone或Marionette相关功能时,每个单独的模块文件的顶部如下所示:

var $ = require('jquery');
var _ = require('underscore');
var Backbone = require('backbone'); Backbone.$ = $;
var Marionette = require('backbone.marionette');

Is there a way do not have to define this at the top of every file? 有没有一种方法不必在每个文件的顶部都定义它? I want my code to be as DRY as possible. 我希望我的代码尽可能地干燥。

Indeed the DRY principle is against this, but unfortunately there is nothing within Browserify for setting global modules. 确实, DRY原则与此相反,但是不幸的是, Browserify中没有设置全局模块的内容。 Because of this, setting global modules would be possible if you would append those modules to global or window but even with the risk of going a bit off-track with the DRY principle, the way you're doing it now is still better than using global or window which is a bad practice 因此,如果您可以将全局模块附加到globalwindow ,则可以设置全局模块,但是即使使用DRY原理可能会有偏离轨道的风险,您现在做的方法还是比使用DRY更好。 globalwindow ,这是一个不好的做法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM