【问题标题】:Having trouble making my imported modules be const无法使我的导入模块成为 const
【发布时间】:2021-10-29 06:10:36
【问题描述】:

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

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

现在,我知道var 不受欢迎,我们喜欢const,而且这种导入确实是不变的。但是,如果我使用:

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

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

索姆

  • 我应该坚持使用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

【问题讨论】:

  • 我们能看到整个错误堆栈吗?
  • @JoeMoore:当然。

标签: javascript module constants thunderbird redeclaration


【解决方案1】:

试试这个:

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

This thread 可以提供帮助。


据我所知,这增加了一种导入 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");

【讨论】:

  • 我不明白这段代码到底是做什么的。能再详细点吗?
  • @einpoklum 我在同一天添加了一个编辑,不知道你有没有看到。
  • 谢谢,我会在下一轮工作时试一试(不是立即:-()
  • @einpoklum 不用担心,让我随时了解进度。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-09-06
  • 1970-01-01
  • 2022-01-22
  • 1970-01-01
  • 2012-11-06
相关资源
最近更新 更多