“导出到浏览器”是什么意思?
backbone.js 带注释的源代码将以下代码描述
var Backbone;
if (typeof exports !== 'undefined') {
Backbone = exports;
} else {
Backbone = root.Backbone = {};
}
为“顶部- level 命名空间。所有公共 Backbone 类和模块都将附加到该命名空间以供 CommonJS 和浏览器使用。”
在这种情况下,“为浏览器导出”是什么意思?
The backbone.js annotated source describes the following piece of code
var Backbone;
if (typeof exports !== 'undefined') {
Backbone = exports;
} else {
Backbone = root.Backbone = {};
}
as "The top-level namespace. All public Backbone classes and modules will be attached to this. Exported for both CommonJS and the browser."
What does "exported for the browser" mean in this context?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在 CommonJS 中,您的模块是隔离的,您想要与需要的事物共享的任何内容都通过“exports”变量共享。例如,Node.js 就使用它。
另一方面,如果您只是在浏览器中,则不使用
exports
变量,而是在root
中添加一个新变量,该变量最终指向 <代码>窗口全局变量。换句话说,如果我们使用支持 CommonJS 的东西,请导出 Backbone。如果没有,请将其放在根上下文中。
In CommonJS, your modules are sequestered and anything you want to share with the thing that requires you is shared through the "exports" variable. Node.js, for instance, uses this.
On the other hand, if you are just in the browser, then you don't use the
exports
variable and you add a new variable inroot
which ultimately points to thewindow
global var.In other words, if we are using something that supports CommonJS, export Backbone. If not, put it in the root context instead.