JavaScript code modules
2020-12-13 03:55
标签:test previous ike like val static only state local Non-standard Note: These are not the same thing as standard JavaScript modules. See JavaScript code modules let multiple privileged JavaScript scopes share code. For example, a module could be used by Firefox itself as well as by extensions, in order to avoid code duplication. JavaScript code modules are a concept introduced in Gecko 1.9 and can be used for sharing code between different privileged scopes. Modules can also be used to create global JavaScript singletons that previously required using JavaScript XPCOM objects. A JavaScript code module is simply some JavaScript code located in a registered location. The module is loaded into a specific JavaScript scope, such as XUL script or JavaScript XPCOM script, using A very simple JavaScript module looks like this: Notice that the module uses normal JavaScript to create functions, objects, constants, and any other JavaScript type. The module also defines a special Array named Note: When you‘re testing changes to a code module, be sure to change the application‘s build ID (e.g., the version) before your next test run; otherwise, you may find yourself running the previous version of your module‘s code. As you can see from the example above, you need a URL to import a code module. (The URL in the example is "resource://app/my_module.jsm".) Code modules can only be loaded using a chrome: (), resource:, or file: URL. An extremely important behavior of Scope1: Scope2: This sharing behavior can be used to create singleton objects that can share data across windows and between XUL script and XPCOM components. Note: Each scope that imports a module receives a by-value copy of the exported symbols in that module. Changes to the symbol‘s value will not propagate to other scopes (though an object‘s properties will be manipulated by reference). Scope1: Scope2: The main effect of the by-value copy is that global variables of simple types won‘t be shared across scopes. Always put variables in a wrapper class and export the wrapper (such as The JavaScript code modules described here are not the same thing as CommonJS modules, but you can import CommonJS modules into any scope where you can use Components.utils.import. Just call the following: This will import You can then use that to import CommonJS modules. You can import Add-on SDK modules in just the same way you could from an SDK add-on: You can import other CommonJS modules, too, as long as you know the path to them: In this case, though, you might be better off creating your own loader, so you can specify the JavaScript code modules 标签:test previous ike like val static only state local 原文地址:https://www.cnblogs.com/chucklu/p/11094509.htmlhttps://developer.mozilla.org/en-US/docs/Mozilla/JavaScript_code_modules
This feature is non-standard and is not on a standards track. Do not use it on production sites facing the Web: it will not work for every user. There may also be large incompatibilities between implementations and the behavior may change in the future.export
and import
to learn more about how to use standard modules.Using JavaScript code modules
Components.utils.import()
or Components.utils["import"]()
.Creating a JavaScript code moduleSection
var EXPORTED_SYMBOLS = ["foo", "bar"];
function foo() {
return "foo";
}
var bar = {
name : "bar",
size : 3
};
var dummy = "dummy";
EXPORTED_SYMBOLS
. Any JavaScript item named in EXPORTED_SYMBOLS
will be exported from the module and injected into the importing scope. For example:Components.utils.import("resource://app/my_module.jsm");
alert(foo()); // displays "foo"
alert(bar.size + 3); // displays "6"
alert(dummy); // displays "dummy is not defined" because ‘dummy‘ was not exported from the module
The URL for a code moduleSection
content
instruction in it, you can put the code module in your content folder and reference it like your other content files via chrome://
Sharing objects using code modulesSection
Components.utils.import()
is that modules are cached when loaded and subsequent imports do not reload a new version of the module, but instead use the previously cached version. This means that a given module will be shared when imported multiple times. Any modifications to data, objects, or functions will be available in any scope that has imported the module. For example, if the simple module were imported into two different JavaScript scopes, changes in one scope can be observed in the other scope.Components.utils.import("resource://app/my_module.jsm");
alert(bar.size + 3); // displays "6"
bar.size = 10;
Components.utils.import("resource://app/my_module.jsm");
alert(foo()); // displays "foo"
alert(bar.size + 3); // displays "13"
Components.utils.import("resource://app/my_module.jsm");
bar = "foo";
alert(bar); // displays "foo"
Components.utils.import("resource://app/my_module.jsm");
alert(bar); // displays "[object Object]"
bar
in the above example).Importing CommonJS modulesSection
const { require } = Cu.import("resource://gre/modules/commonjs/toolkit/require.js", {})
require()
into your scope.// import the SDK‘s base64 module
var base64 = require("sdk/base64");
base64.encode("hello"); // "aGVsbG8="
// import my module
var myModule = require("resource://path/to/my/module.js");
paths
property yourself.