`
zha_zi
  • 浏览: 594066 次
  • 性别: Icon_minigender_1
  • 来自: 西安
社区版块
存档分类
最新评论

CommonJS Contract 原文

 
阅读更多

STATUS: SUPERSEDED BY 1.1

Implementations
FlusspferdGLUEscriptGPSEEJSBuildNarwhal (0.1), PersevereRingoJSSproutCore 1.1/Tikinode.jsTeaJS (formerly v8cgi)CouchDBSmart PlatformYabble,WakandaXULJet

This specification addresses how modules should be written in order to be interoperable among a class of module systems that can be both client and server side, secure or insecure, implemented today or supported by future systems with syntax extensions. These modules are offered privacy of their top scope, facility for importing singleton objects from other modules, and exporting their own API. By implication, this specification defines the minimum features that a module system must provide in order to support interoperable modules.

Contents

 [hide

Contract

Module Context

  1. In a module, there is a free variable "require", that is a function.
    1. The "require" function accepts a module identifier.
    2. "require" returns the exported API of the foreign module.
    3. If there is a dependency cycle, the foreign module may not have finished executing at the time it is required by one of its transitive dependencies; in this case, the object returned by "require" must contain at least the exports that the foreign module has prepared before the call to require that led to the current module's execution.
    4. If the requested module cannot be returned, "require" must throw an error.
  2. In a module, there is a free variable called "exports", that is an object that the module may add its API to as it executes.
  3. modules must use the "exports" object as the only means of exporting.

Module Identifiers

  1. A module identifier is a String of "terms" delimited by forward slashes.
  2. A term must be a camelCase identifier, ".", or "..".
  3. Module identifiers may not have file-name extensions like ".js".
  4. Module identifiers may be "relative" or "top-level". A module identifier is "relative" if the first term is "." or "..".
  5. Top-level identifiers are resolved off the conceptual module name space root.
  6. Relative identifiers are resolved relative to the identifier of the module in which "require" is written and called.

Unspecified

This specification leaves the following important points of interoperability unspecified:

  1. Whether modules are stored with a database, file system, or factory functions, or are interchangeable with link libraries.
  2. Whether a PATH is supported by the module loader for resolving module identifiers.

Unit Tests

Sample Code

math.js
exports.add = function() {
    var sum = 0, i = 0, args = arguments, l = args.length;
    while (i < l) {
        sum += args[i++];
    }
    return sum;
};
increment.js
var add = require('math').add;
exports.increment = function(val) {
    return add(val, 1);
};
program.js
var inc = require('increment').increment;
var a = 1;
inc(a); // 2

Notes

分享到:
评论

相关推荐

    commonjs,commonjs9

    扩展的EXTJS公共类,扩展的EXTJS公共类,扩展的EXTJS公共类

    commonjs,commonjs3

    扩展的EXTJS公共类,扩展的EXTJS公共类,扩展的EXTJS公共类

    commonjs,commonjs1

    扩展的EXTJS公共类,扩展的EXTJS公共类,扩展的EXTJS公共类

    commonjs,commonjs10

    扩展的EXTJS公共类,扩展的EXTJS公共类,扩展的EXTJS公共类

    rollup-plugin-commonjs, 将CommonJS模块转换为 ES2015.zip

    rollup-plugin-commonjs, 将CommonJS模块转换为 ES2015 rollup-plugin-commonjs 将CommonJS模块转换为 ES6,以便它们可以包含在Rollup包中安装npm install --save-dev rollup-plugin-commonjs用法

    commonjs,commonjs8

    扩展的EXTJS公共类,扩展的EXTJS公共类,扩展的EXTJS公共类

    一个Babel7转换插件用于将CommonJS转换为ESM

    本篇文章将深入探讨Babel 7中的转换插件,特别是如何使用`babel-plugin-transform-commonjs`将CommonJS转换为ESM。 首先,让我们了解CommonJS和ESM的区别。CommonJS采用同步导入机制,允许在代码中使用`require()`...

    CommonJS时代的JS模块和编译工具

    CommonJS是一种在服务器端JavaScript中实现模块化的规范,它的出现主要是为了标准化服务器端JavaScript模块的加载机制。CommonJS规范最初于2009年提出,它的设计理念是基于同步加载机制,并且以文件为模块的载体。在...

    commonjs-everywhere, 具有别名,可扩展性和源映射的CommonJS浏览器 bundler.zip

    commonjs-everywhere, 具有别名,可扩展性和源映射的CommonJS浏览器 bundler 到处都是 web浏览器使用从缩小的JS包到原始源的源代码,浏览器替代的别名和任意语言支持的可扩展性。安装npm install -g commonjs-...

    nashorn-commonjs-modules, CommonJS的模块支持 Nashorn.zip

    nashorn-commonjs-modules, CommonJS的模块支持 Nashorn CommonJS模块支持 Nashorn这个库增加了对CommonJS模块( aka require ) inside的支持( Nashorn脚本引擎) 。 它基于 NodeJS模块的规范,它支持从文件夹加载模块...

    commonjs,commonjs7

    扩展的EXTJS公共类.扩展的EXTJS公共类.扩展的EXTJS公共类

    01CommonJS.rar

    CommonJS是Node.js中实现模块化开发的重要规范,它允许我们在JavaScript中导入和导出模块,使得代码结构清晰、可复用性增强。本文将深入探讨CommonJS的基本概念、使用方法以及通过一个模拟扑克牌发牌的小例子来实践...

    CommonJS和ES6模块的区别

    ### CommonJS与ES6模块的区别详解 #### 一、引言 随着JavaScript的发展与应用领域的扩展,特别是Node.js的出现,让JavaScript不仅局限于浏览器环境,更拓展到了服务器端。为了更好地管理和组织代码,模块化成为了...

    JS中的模块规范(CommonJS,AMD,CMD&#40;少用&#41;)

    #### 一、CommonJS **CommonJS** 是为了解决JavaScript在实际应用中缺乏模块化支持的问题而诞生的一种规范。它最初是为了让JavaScript能够在服务器端运行,尤其是为了适应Node.js这样的环境。CommonJS的出现极大地...

    rollup-plugin-commonjs:此模块已移动,现在可通过@ rollupplugin-commonjs https获得

    汇总插件-commonjs 将CommonJS模块转换为ES6,以便可以将它们包含在汇总包中安装npm install --save-dev rollup-plugin-commonjs用法通常,您可以将此插件与一起使用,以便将CommonJS依赖项捆绑在node_modules 。...

    JavaScript:模块化编程与CommonJS技术详解

    内容概要:本文详细介绍了JavaScript的模块化编程及其在CommonJS中的实现方法。首先解释了模块化编程的重要性和在JavaScript中的应用,接着具体讲述了CommonJS的规范和实现细节,包括模块的定义、导出、导入及缓存...

    commonjs1.part04.rar

    commonjs1.part04.rar

    commonjs1.part02.rar

    commonjs1.part02.rar

Global site tag (gtag.js) - Google Analytics