CommonJS

做者:http://raychase.iteye.com/blog/1463617javascript

 

老实说,以前我对CommonJS也是一无所知,直到不久前Node.js火起来,我去研究它,才了解到Node.js实际上是CommonJS的一个部分实现,我才关注起CommonJS来。前端

 

 

之前咱们提及HTML,咱们提及JavaScript,你们的印象彷佛都停留在了“网页”上面,从2005年开始,由于Ajax的缘故,有一种叫作“前端程序员”的新型物种诞生了。java

但是如今,前端程序员不甘心本身的一亩三分地了,他们要越权,看看HTML 5的API吧,它是要吃了Flash啊,连Adobe都倒向HTML 5了,它的API能够作各类各样的事情,要绘图、操做文件、客户端数据库、摄像头、重力感应……这些特性,怎么看怎么都是一个Native App,或者是一个服务端的应用才具有的啊。git

另外一方面,在2009年8月,CommonJS诞生了(不妨读读这篇文章)。正是站在JavaScript的层面,它要作这样一件相似的事情(The CommonJS API will fill that gap by defining APIs that handle many common application needs, ultimately providing a standard library as rich as those of Python, Ruby and Java.),同时伴随着JavaScript的解释器越来越成熟,性能愈来愈强,JavaScript的威力再也不仅仅被限制在浏览器端。程序员

 

  • 服务端JavaScript应用
  • 命令行工具
  • 图形界面应用
  • 混合应用(Titanium、Adobe AIR等)

这就是CommonJS的着力点。从CommonJS的官网来看,它至始至终都避免使用“规范”这个词,彷佛不想自认,但个人理解,CommonJS本质上就是一个规范,它并不提供默认实现,而是要求一些JavaScript库、框架、环境……去实现它的这些API定义。github

 

那么,都有哪些API层面的定义呢?web

  • binary: Binary Data Objects (byte arrays and/or strings) (proposals, discussion, early implementations)
  • encodings: Encodings and character sets (proposals, discussion, early implementations)
  • io: I/O Streams (proposals, discussion)
  • fs, fs-base: Filesystem (proposals, discussion, early implementations)
  • system: System Interface (stdin, stdout, stderr, &c) (1.0, amendments proposed)
  • assert, test: Unit Testing (1.0, amendment proposals pending)
  • sockets: Socket I/O TCP/IP sockets (early proposals)
  • event-queue: Reactor Reactor/Event Queue (early proposals)
  • worker: Worker Worker (concurrent shared nothing process/thread) (proposal)
  • console: console (proposal)

 

OK,涉及到二进制、编码、IO、文件、系统、断言测试、套接字、事件队列、Worker、控制台等等实现,偏偏弥补的传统JavaScript的空白。数据库

 

就以Node.js为例,它实现了CommonJS的以下子规范:浏览器

  • Modules/1.0 (yes)
  • Promises/B (http://github.com/kriskowal/q)
  • Promises/D (https://github.com/kriskowal/q)
  • Unit Testing/1.0 (yes)

再如大名鼎鼎的SeaJS、CouchDB和RequireJS,都是CommonJS的部分实现并发

若是你有兴趣,在这里能够找到CommonJS全部的子规范。

 

将来CommonJS还须要加入的底层API定义包括(语言和运行时环境、日志、关系数据库接口、结果集、并发、字符串/字节序列/IO、目标平台的C语言标准统一的API和子进程):

  • Language and Runtime Services
  • Logging
  • Relational database interface
  • ResultSets (collections of data maybe from RDBMS, maybe from other sources)
  • Concurrency
  • String / ByteString I/O
  • C unified API to our Target Platforms
  • Subprocesses (popen)

高层API包括(HTTP客户端API、Email、Jabber、国际化、Promise管理器、命令行处理):

  • HTTP client APIs
  • Email
  • Jabber (XMPP)
  • Internationalization
  • Promise Manager
  • Command line processing

这些均可以在CommonJS本身的Wiki上找到。

 

按照CommonJS小组本身的话来讲,在CommonJS 0.5版本的开发过程当中,已经讨论了API须要包含的范围,创建了一个收集提议和优先级信息的Wiki页,讨论了现有的提议并排序,达成了基本一致的意见,提议都已在Wiki上定稿;如今当务之急包括给应用设计合理的API并文档化,给这些API完成更多的实现等等。

相关文章
相关标签/搜索