导入模块为const时遇到麻烦



我正在用Javascript更新一个(Thunderbird)扩展。在我的JS文件中,我有:

var { ObjectUtils } = ChromeUtils.import("resource://gre/modules/ObjectUtils.jsm");

现在,我知道var是不受欢迎的,而我们喜欢const,并且进口确实是恒定的。但是,如果我使用:

const { ObjectUtils } = ChromeUtils.import("resource://gre/modules/ObjectUtils.jsm");

我得到关于重新定义ObjectUtils的错误,可能是当多个JS文件包含在我的XUL/XHTML中具有相同的行时。

Som

  • 我应该坚持使用var吗?
  • 我应该写:
    if ("undefined" == typeof(ObjectUtils)) {
    const { ObjectUtils } = ChromeUtils.import("resource://gre/modules/ObjectUtils.jsm");
    }
    
    ?
  • 我应该做点别的吗?

根据流行的请求,这里是堆栈:

redeclaration of var ObjectUtils removedupes.js:1
<anonymous> chrome://removedupes/content/removedupes.js:1
<anonymous> chrome://removedupes/content/overlay-injectors/messenger.js:5
_loadIntoWindow jar:file:///home/eyalroz/.thunderbird/Profiles/8shkz5up.default/extensions/{a300a000-5e21-4ee0-a115-9ec8f4eaa92b}.xpi!/api/WindowListener/implementation.js:968
onLoadWindow jar:file:///home/eyalroz/.thunderbird/Profiles/8shkz5up.default/extensions/{a300a000-5e21-4ee0-a115-9ec8f4eaa92b}.xpi!/api/WindowListener/implementation.js:687
checkAndRunExtensionCode resource:///modules/ExtensionSupport.jsm:220
_checkAndRunMatchingExtensions resource:///modules/ExtensionSupport.jsm:192
registerWindowListener resource:///modules/ExtensionSupport.jsm:71
forEach self-hosted:4357
registerWindowListener resource:///modules/ExtensionSupport.jsm:70
startListening jar:file:///home/eyalroz/.thunderbird/Profiles/8shkz5up.default/extensions/{a300a000-5e21-4ee0-a115-9ec8f4eaa92b}.xpi!/api/WindowListener/implementation.js:569
startListening self-hosted:1175
result resource://gre/modules/ExtensionParent.jsm:935
withPendingBrowser resource://gre/modules/ExtensionParent.jsm:491
result resource://gre/modules/ExtensionParent.jsm:935
callAndLog resource://gre/modules/ExtensionParent.jsm:897
recvAPICall resource://gre/modules/ExtensionParent.jsm:934
InterpretGeneratorResume self-hosted:1482
AsyncFunctionNext self-hosted:692

试试这个:

const { ObjectUtils: Cu } = ChromeUtils;
Cu.import("resource://gre/modules/ObjectUtils.jsm");

这个线程可能会有帮助。


从我可以收集这增加了一种方法来导入ObjectUtils没有分配常量的值(你不能在运行时改变)。

我是这样想的:

// say you create a constant array (I know its not an array - just an example)
const cars = ["Saab", "Volvo", "BMW"];
// you can add an element, just like you can add an import
cars.push("Audi");

相关内容

  • 没有找到相关文章

最新更新