Firefox 扩展 JSM 和命名空间礼仪
因此,在 Firefox 扩展中,鼓励扩展的对象位于 com.contoso.myExtension 等子对象中。这样,您就不会在全局名称空间中放置任何对象,并且扩展通常不会相互干扰。 (至少在通用 browser.xul 窗口中)
那么使用 JSM 时要发挥良好作用的协议是什么?只是创建很长的变量名称并希望它们不会发生冲突?
So in Firefox extensions it's encouraged for your extension's objects to live in sub-objects like com.contoso.myExtension . That way you have not put any objects in the global namespace and extensions generally stay out of each other's hair. (At least in the common browser.xul window)
But from what I have understand about Javascript code modules (JSMs), is that while the module itself is working in a separate namespace, the symbols that it exports will end up in the global namespace of whatever code imports it. Furthermore, it's impossible for an extension to be "nice" and only try to build sub-objects; those exported symbols will just whack whatever global variables already existed. Also you can't export a symbol like com.contoso.myExtension. It's only a simple global variable.
So what's the protocol for playing nice when using JSMs? Just make really long variable names and hope they won't collide?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,我还没有看到如何处理这个问题的硬性标准。但我们绝对可以做得比长变量名更好...
关于 Javascript 代码模块位于单独的命名空间(可以这么说)中,您是正确的,但是当您导入它们时,您不会不必将它们导入全局名称空间。如果您查看 Components.utils.import 文档,您会发现可以导入到特定范围。也就是说,您根本不必污染全局命名空间。
您可以将模块收集到
myExtension
命名空间中。将其包装在自执行函数中不会让任何变量泄漏到全局命名空间中,甚至
myExtension
也不行!First off, I haven't seen a hard a true standard for how to handle this. But we can definitely do much better than just long variable names...
You are correct about the Javascript Code Modules living in a separate namespace (so to speak), however when you import them, you don't have to import them into the global namespace. If you look at the Components.utils.import documentation, you see that you can import onto a specific scope. That is, you don't have to pollute the global namespace at all.
You can collect your modules into a
myExtension
namespace.And wrapping this inside a self-executing function doesn't let any variables leak into the global namespace, not even
myExtension
!