einpoklum
einpoklum

Reputation: 132300

Having trouble making my imported modules be const

I'm working on updating a (Thunderbird) extension in Javascript. In a JS file of mine, I have:

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

now, I know that var is frowned upon, and that we like const, and that import will indeed be constant. However, if I use:

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

I get errors about redefining ObjectUtils, probably when multiple JS file are included from my XUL/XHTML that have the same line in them.

Som


Per popular request, here's the stack:

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

Upvotes: 0

Views: 214

Answers (1)

Joe Moore
Joe Moore

Reputation: 2023

Try this:

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

This thread could help.


From what I can gather this adds a way to import ObjectUtils without assigning the constant a value (that you cannot change during runtime).

I'm thinking about it like this:

// 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");

Upvotes: 0

Related Questions