JavaScript code modules

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.

原文地址:https://www.cnblogs.com/chucklu/p/11094509.html

时间: 2024-10-09 13:49:05

JavaScript code modules的相关文章

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

废话少说只就上Code: 说明:getDay()方法获取星期(这里的星期是从0到6).参见:http://www.w3school.com.cn/js/js_obj_date.asp 中的getDay(). 代码有不足之处希望得到指正. var SecondFriday = { getSecondFriday: function () { var flag = 0; //(1) 获取当月月初时间,时间格式为:Sun Jun 01 2014 00:00:00 GMT+0800 (中国标准时间) v

JavaScript code to let users double-click to move back to the top of the page

Here's some JavaScript code I found today while poking around the net. Embed this JavaScript code in a web page, and a user can simply double-click anywhere on the page to automatically move back to the beginning of the page. Pretty cool idea. Here's

Random Javascript code snippets

MollyPages.org"You were wrong case.To live here is to live." Home Pages / Database / Forms / Servlet / Javadocs / License & Download / Tutorials / Cookbook / Contact Return to Tutorials index Random collection of misc. code and snippets Priv

Server Side JavaScript Code Injection Attack服务端js注入攻击

今天扫描器误报了这个漏洞,我觉着是误报了. 趁机了解一下, 好像是针对nosql与nodejs的服务端, 我觉着可能是js对于nodejs就是可执行的代码, 也就是任意代码执行, 这么一个攻击. stackoverflow上有一个http://stackoverflow.com/questions/27879131/server-side-javascript-code-injection-attack 巧了,看来我和他用了同款扫描工具,有了同样的问题. 看回答,大意竟是赞同他aspx可能有这个

[javascript]Three parts of javascript code snippet

<script> (function(){ /* if (navigator.userAgent.toLowerCase().indexOf("iphone") == -1){ $("#id_download_btn img").attr("src" , "http://cache.hinabian.com/mobile/images/downLoadLink.png"); return; } */ var old

Code Conventions for the JavaScript Programming Language

This is a set of coding conventions and rules for use in JavaScript programming. It is inspired by the Sun document Code Conventions for the Java Programming Language. It is heavily modified of course because JavaScript is not Java. The long-term val

【转】3 Essential Sublime Text Plugins for Node &amp; JavaScript Developers

原文转自:http://scottksmith.com/blog/2014/09/29/3-essential-sublime-text-plugins-for-node-and-javascript-developers/ Check out these 3 great and essential Sublime Text plugins every JavaScript and Node developer should know about and use. JsFormat https:

Isomorphic JavaScript: The Future of Web Apps

Isomorphic JavaScript: The Future of Web Apps At Airbnb, we’ve learned a lot over the past few years while building rich web experiences. We dove into the single-page app world in 2011 with our mobile web site, and have since launched Wish Lists and

15款加速 Web 开发的 JavaScript 框架

JavaScript 可以通过多种方式来创建交互式的网站和 Web 应用程序.利用 JavaScript,可以让你移动 HTML 元素,创建各种各样的自定义动画,给你的访问者更好的终端用户体验. 对于开发人员你来说,有无数的 JavaScript 框架可选择,往往是很难选择最适合您需要的.所以在这篇文章中,我收集了15个有用的和流行的 JavaScript 框架,旨在简化前端应用程序开发. 您可能感兴趣的相关文章 期待已久的2013年度最佳 jQuery 插件揭晓 小伙伴们都惊呆了!8个超炫的