CommonJS Contract 原文
来源:互联网 发布:php redirect传递参数 编辑:程序博客网 时间:2024/05/01 15:58
STATUS: SUPERSEDED BY 1.1
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]- 1 Contract
- 1.1 Module Context
- 1.2 Module Identifiers
- 1.3 Unspecified
- 2 Unit Tests
- 3 Sample Code
- 4 Notes
- 5 Amendment Proposals
- 6 Implementations
- 7 Related Documents
Contract
Module Context
- In a module, there is a free variable "require", that is a function.
- The "require" function accepts a module identifier.
- "require" returns the exported API of the foreign module.
- 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.
- If the requested module cannot be returned, "require" must throw an error.
- 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.
- modules must use the "exports" object as the only means of exporting.
Module Identifiers
- A module identifier is a String of "terms" delimited by forward slashes.
- A term must be a camelCase identifier, ".", or "..".
- Module identifiers may not have file-name extensions like ".js".
- Module identifiers may be "relative" or "top-level". A module identifier is "relative" if the first term is "." or "..".
- Top-level identifiers are resolved off the conceptual module name space root.
- 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:
- Whether modules are stored with a database, file system, or factory functions, or are interchangeable with link libraries.
- Whether a PATH is supported by the module loader for resolving module identifiers.
Unit Tests
- Unit Tests at Google Code by Kris Kowal
- Unit Tests Git Mirror by Ash Berlin
Sample Code
exports.add = function() { var sum = 0, i = 0, args = arguments, l = args.length; while (i < l) { sum += args[i++]; } return sum;};
var add = require('math').add;exports.increment = function(val) { return add(val, 1);};
var inc = require('increment').increment;var a = 1;inc(a); // 2
Notes
0 0
- CommonJS Contract 原文
- contract
- Contract
- CommonJS
- CommonJS
- CommonJS
- commonjs
- CommonJs
- 原文
- Contract Test
- [WCF] Contract
- [WCF] Contract
- 合同 Contract
- Contract简介
- Bilateral Contract
- CommonJS调研
- CommonJS简介
- CommonJS规范
- HTML页面加载和解析流程
- HTTP请求中浏览器的缓存机制
- 浏览器小知识总结
- sessionStorage 和localStorage 区别
- jsonp 跨域原理详解
- CommonJS Contract 原文
- 深入理解浏览器兼容性模式
- insertBefore 的变态用法
- js 高级编程
- html5 游戏优劣
- #import时<>括号和双引号的区别
- js变量提升
- setinterval 使用闭包解决参数不能传递的问题
- 集群中session同步解决方式