https://developer.mozilla.org/en-US/docs/Mozilla/JavaScript_code_modules

Non-standard
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.

Note: These are not the same thing as standard JavaScript modules. See export and import to learn more about how to use standard modules.

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.

Using JavaScript code modules

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 Components.utils.import() or Components.utils["import"]().

Creating a JavaScript code moduleSection

A very simple JavaScript module looks like this:

var EXPORTED_SYMBOLS = ["foo", "bar"];

function foo() {
return "foo";
} var bar = {
name : "bar",
size : 3
}; var dummy = "dummy";

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 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

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.

The URL for a code moduleSection

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.

  • If you're writing an extension for Firefox 4 and already have a chrome.manifest with a content instruction in it, you can put the code module in your content folder and reference it like your other content files via chrome://<yourextension>/content/<yourmodule>.jsm.
  • If your extension or application needs to support Mozilla 1.9.x (Firefox 3.x), you should register a new resource URL. Details on doing this are in the "Extending resource: URLs" section below.

Sharing objects using code modulesSection

An extremely important behavior of 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.

Scope1:

Components.utils.import("resource://app/my_module.jsm");

alert(bar.size + 3);  // displays "6"

bar.size = 10;

Scope2:

Components.utils.import("resource://app/my_module.jsm");

alert(foo());         // displays "foo"
alert(bar.size + 3); // displays "13"

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:

Components.utils.import("resource://app/my_module.jsm");

bar = "foo";
alert(bar); // displays "foo"

Scope2:

Components.utils.import("resource://app/my_module.jsm");

alert(bar);         // displays "[object Object]"

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 bar in the above example).

Importing CommonJS modulesSection

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:

const { require } = Cu.import("resource://gre/modules/commonjs/toolkit/require.js", {})

This will import require() into your scope.

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:

// import the SDK's base64 module

var base64 = require("sdk/base64");
base64.encode("hello"); // "aGVsbG8="

You can import other CommonJS modules, too, as long as you know the path to them:

// import my module

var myModule = require("resource://path/to/my/module.js");

In this case, though, you might be better off creating your own loader, so you can specify the paths property yourself.

 

JavaScript code modules的更多相关文章

  1. Eloquent JavaScript #10# Modules

    索引 Notes 背景问题 模块Modules 软件包Packages 简易模块 Evaluating data as code CommonJS modules ECMAScript modules ...

  2. Javascript Code Style Guide

    本指南采用的Airbnb发布的基于ES5的JavaScript Code Style. ES5 英文版:https://github.com/airbnb/javascript/tree/es5-de ...

  3. jshint-eclipse: JavaScript Code Quality Plugin for Eclipse

    https://blog.oio.de/2012/03/26/jshint-eclipse-javascript-code-quality-plugin-for-eclipse/   techscou ...

  4. Random Javascript code snippets

    MollyPages.org"You were wrong case.To live here is to live." Home Pages / Database / Forms ...

  5. javascript code snippet -- Forwarding Mouse Events Through Layers

    Anyone who has worked with web apps has likely created a masking element at some point, and the grea ...

  6. javaScript Code 用javascript确定每月第二个星期五

    废话少说只就上Code:     说明:getDay()方法获取星期(这里的星期是从0到6).参见:http://www.w3school.com.cn/js/js_obj_date.asp 中的ge ...

  7. [javascript]Three parts of javascript code snippet

    <script> (function(){ /* if (navigator.userAgent.toLowerCase().indexOf("iphone") == ...

  8. Javascript code for soft keyboard

    <style>     BODY {     SCROLLBAR-FACE-COLOR: #f0f0f6; FONT-SIZE: 9pt; BACKGROUND-ATTACHMENT: f ...

  9. JavaScript code 性能优化

    1 1 1 JavaScript 性能优化 prototype 闭包 Closure 内存泄漏 event system 1 定义类方法以下是低效的,因为每次构建baz.Bar的实例时,都会为foo创 ...

随机推荐

  1. Linux的磁盘配额详解(Quota)

    1. 检查内核情况 检查当前内核是否支持quota,当前内核配置文件在/boot下 如果当前内核不支持quota,需要重新编译内核将quota support编译进核心: File systems   ...

  2. layui动态渲染select等组件并初始化赋值失败

    描诉:有一个用户信息form表单,其中有部门单选框,数据库中有一张dept(部门)表,要动态渲染出所有部门,并默认选中用户所在部门 关键代码: html页面 <div class="l ...

  3. linux中断处理上下部分

    一.linux中断处理为什么要分为上下部 1.1. 中断处理的上半部(top half,又叫顶半部)和处理的下半部(bottom half,又叫底半部) 1.1. linux中断处理不参与调度,故中断 ...

  4. selenium 教程

    selenium 本身是一套web自动化测试工具,但其经常被用于爬虫,解决一些复杂爬虫的问题. selenium 用于爬虫时,相当于模拟人操作浏览器. 浏览器驱动 使用 selenium 需要先安装 ...

  5. 使用Eclipse-Maven-git做Java开发(3)--Eclipse的安装和配

    使用Eclipse-Maven-git做Java开发(3)--Eclipse的安装和配 https://my.oschina.net/songxinqiang/blog/474530

  6. input 限制 中文输入

    ime-mode:disabled是什么? 解决: 1.     ime-mode版本:IE5+专有属性 继承性:无    语法:     ime-mode : auto | active | ina ...

  7. MySQL存储过程、触发器、自定义函数、事务

    1.存储过程 MySQL中存储过程的参数中有IN.OUT.INOUT类型,但是函数的参数只能是IN类型的. “in” 参数:跟 C 语言的函数参数的值传递类似, MySQL 存储过程内部可能会修改此参 ...

  8. VB中RaiseEvent语句的功能及用法

    Creat a new class named Class1, it's codes like this: Public Event MyEvent() Public Sub RaiseTheEven ...

  9. Linux之目录配置

    Linux目录配置标准:FHS 主要目的,希望让用户可以了解到已安装软件通常放置于哪个目录下. FHS定义了三层主目录:/./usr./var 1. /(root,根目录) (1)根目录与开机.还原. ...

  10. 最近关于Qt学习的一点碎碎念

    最近关于Qt学习的一点碎碎念 一直在使用Qt,但是最近对Qt的认识更加多了一些.所以想把自己的一些想法记录下来. Qt最好的学习资料应该是官方的参考文档了.对Qt的每一个类都有非常详细的介绍.我做了一 ...