`

Modules/1.0[翻译]

阅读更多
Modules/1.0

STATUS: SUPERSEDED BY 1.1

状态: 被Modules/1.1取代


Implementations:
    Flusspferd, GLUEscript, GPSEE, JSBuild, Narwhal (0.1), Persevere, RingoJS, SproutCore 1.1/Tiki, node.js, TeaJS (formerly v8cgi), CouchDB, Smart Platform, Yabble, Wakanda, XULJet

实现实例:
    Flusspferd, GLUEscript, GPSEE, JSBuild, Narwhal (0.1), Persevere, RingoJS, SproutCore 1.1/Tiki, node.js, TeaJS (formerly v8cgi), CouchDB, Smart Platform, Yabble, Wakanda, XULJet


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 features that a module system must provide in order to support interoperable modules.

本文提出如何写出可以交互的模块,这类模块组成的系统将可以在客户端和服务器端运行,可以是安全的或不安全的,可以是已经实现,抑或者需要在未来的语言扩展后的系统中才能支持。这些模块将保证高度的封装,其他模块通过单例的对象保证引入的灵巧性,以及模块API的输出。本文定义了用以模块间交互的模块系统所必有的功能。


Module Context
  1. In a module, there is a free variable "require", that is a function.
  2.          1.1 The "require" function accepts a module identifier.
             1.2 "require" returns the exported API of the foreign module.
             1.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.
             1.4 If the requested module cannot be returned, “require” must throw an error.
  3. 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.
  4. modules must use the "exports" object as the only means of exporting.


模块内容
  1. 在一个模块中,有一个“require”的自由变量,“require”是一个如下的方法。
  2.          1.1 “require”方法接受模块定义。
             1.2 “require”返回外部模块输出地API。
             1.3 如果含有依赖循环,外部模块可能在没有执行完的时候需要引入其他的需要引入一个它需要的依赖;在这种情况,“require”返回的对象必须至少含有输出,这些输出是由外部模块提供,而且必须在当前模块需要执行前被引入。
             1.4 如果被请求的模块没有返回值,则“require”必须抛出一个错误。
  3. 一个模块中,需要含有一个自由变量“exports”,模块会将它的API作为对象加入到“exports”中执行。
  4. 模块将使用“exports”对象作为唯一的输出。



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.


模块定义
  1. 一个模块的定义,是指一串“term”组成的字符串,而这些“term”是被斜杠分割开来的。
  2. 一个term必须是驼峰写法(camelCase)的定义,“.”或“..”。
  3. 模块定义可以不必含有类似于“.js”的扩展名。
  4. 模块定义可以是“相对的”或“顶层的”(应该是指相对路径和绝对路径)。模块定义如果第一个term是“.”或者“..”,则其必是“相对的”。
  5. 顶层模块的定义由概念模块中命名空间的根所解决。
  6. 相对模块的定义是相对于定义的模块require被调用时位置决定的。



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

未指定
本文对于以下几点很重要的交互性操作并未说明:
模块的存储方式:是数据库,是文件系统,是工厂方法,还是可交互的链接库。
用于解决模块定义的加载器是否支持路径。


代码例子:
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
分享到:
评论

相关推荐

    Error:in ./node-modules/axios/lib/platform/index.js

    Error:in ./node-modules/axios/lib/platform/index.js

    解决webpack4+升级webpack5+ vue-pdf不可用问题

    ERROR in ./node_modules/vue-pdf/node_modules/pdfjs-dist/es5/build/pdf.worker.js (./node_modules/vue-pdf/node_modules/worker-loader/dist/cjs.js!./node_modules/vue-pdf/node_modules/pdfjs-dist/es5/build/...

    go-bitbucket-v1:用于Golang的Bitbucket服务器API(v1.0)库

    用于Bitbucket的Go API客户端(bitbucket-server V1)概述Atlassian的Bitbucket服务器(以前的存储)的API客户端。...groups DefaultApi 发布/api/1.0/admin/groups/add-users DefaultApi发布/api/1.0/projects/

    tomcat_apache共享动态连接库

    LoadFile modules/libz.so LoadModule authn_file_module modules/mod_authn_file.so LoadModule authn_dbm_module modules/mod_authn_dbm.so LoadModule authn_anon_module modules/mod_authn_anon.so LoadModule ...

    node-modules/@wangeditor/editor-for-vue

    node_modules/@wangeditor/editor-for-vue

    apollo/modules/planning/common/path/DiscretizedPath类单元测试的cmake实现

    apollo/modules/planning/common/path/DiscretizedPath类单元测试的cmake实现 apollo原本是通过bazel的方式进行编译,同时测试脚本与其他模块耦合度高,这里将DiscretizedPath类相关代码全部截出单独测试 ...

    若依 cms 博客下载下来的前端代码,启动报错 node版本16.17.0. npm 8.15.0?

    error in ./node_modules/cherry-markdown/dist/cherry-markdown.esm.js Module parse failed: Unexpected token (1:1804289) You may need an appropriate loader to handle this file type, currently no ...

    jiaminghi/data-view dataV for vite2 vue3 windows补丁 for pnpm

    官网直接安装的不支持vite2+vue3的 主要修复: 1.build或者dev项目时不报错,兼容vite2...2.将other_modules压缩包解压并覆盖到项目的node_modules文件夹下即可 调用方法,请参考官网文档http://datav.jiaminghi.com/

    OpenWRT_LuCI

    luci/src/modules/base/luasrc/tools/status.lua luci/src/modules/base/luasrc/model/network.lua luci/src/modules/admin-full/luasrc/controller/admin/network.lua luci/src/modules/admin-full/luasrc/...

    No package &#039;orocos-bfl&#039; found

    CMake Error at /usr/share/cmake-3.5/Modules/FindPkgConfig.cmake:367 (message): A required package was not found Call Stack (most recent call first): /usr/share/cmake-3.5/Modules/FindPkgConfig.cmake:...

    安装lamp报错信息及解决方法

    httpd: Syntax error on line 57 of /etc/httpd/httpd.conf: Cannot load /usr/local/apache2/modules/libphp5.so into server: /usr/local/apache2/modules/libphp5.so: undefined symbol: xmlTextReaderSetup ...

    编译ubuntu驱动.TXT

    (3)没有 /lib/modules/*/build 这个目录,make报错(4)内核编译 CONFIG_MODVERSIONS 作用(5)安装内核源码与相应的头文件(6)载入驱动模块(7)/etc/rc.local 不执行的问题(8)内核升级后,驱动程序要升级,为...

    node_modules

    NULL 博文链接:https://lsqwzz.iteye.com/blog/2008386

    NCVPixelOperations.hpp ,opencv编译出错

    opencv-2.4.9/modules/gpu/src/nvidia/core/NCVPixelOperations.hpp(51): error: a storage class is not allowed in an explicit specialization 这是由于NCVPixelOperations.hpp的问题,只要替换opencv-2.4.9/...

    opencv-4.2.0.tar.gz

    opencv4.2的资源,不cross wall太难了,所以在csdn上面进行一下相应备份,并无特殊其他用途。编译方面可参考

    jiaminghi/data-view dataV for vite vue2 补丁 for npm/yarn

    2.将other_modules压缩包解压并覆盖到项目的node_modules文件夹下即可 调用方法,请参考官网文档http://datav.jiaminghi.com/ npm下也可以通过patch-package库包自行定制补丁,下载npm install时就不用手动复制了,...

    es6.core.js

    ES6 Core Symbol 一个不可变的数据类型,用来产生唯一的标识,但是却无法直接访问这个标识 Promise 异步编程函数 Set 有序列表集合,它不会包含重复项 WeakSet 列表集合,和Set一样,只不过它的值只能是非空对象 ...

    TL-WN725N 1.0 Linux驱动补丁

    解决tp-link usb 无线网无法...make -C /lib/modules/$(uname -r)/build/ M=$(pwd) modules 插入usb无线网卡 # sudo rmmod mt7601u 拔出usb无线网卡 # sudo insmod ./mt7601u.ko vnd_reset=0 再次插入usb无线网卡即可

    jiaminghi/data-view dataV for vite2 vue3 windows补丁 for npm/yarn

    官网直接安装的不支持vite2+vue3的 主要修复: 1.build或者dev项目时不报错,兼容vite2...2.将other_modules压缩包解压并覆盖到项目的node_modules文件夹下即可 调用方法,请参考官网文档http://datav.jiaminghi.com/

    H5打开pdf格式文件,实现原理canvas

    通过canvas画布解析pdf,生成base64,绘制到页面,直接以链接的形式访问此页面,只需要地址栏拼接file=****(pdf路径)

Global site tag (gtag.js) - Google Analytics