CommonJS Browser Implementation

Source: Internet
Author: User

Commonjs

http://www.commonjs.org/

CommonJS

JavaScript is a powerful object oriented language with some of the fastest dynamic language interpreters around. The official JavaScript specification defines APIs for some objects that is useful for building browser-based application S. However, the spec does not define a standard library, which is useful for building a broader range of applications.

The CommonJS API would fill that gap by defining APIs that handle many common application needs, ultimately providing a STA Ndard Library as rich as those of Python, Ruby and Java. The intention is, an application developer would be able to write a application using the CommonJS APIs and then run T Hat application across different JavaScript interpreters and host environments. With Commonjs-compliant systems, you can use JavaScript to write:

    • Server-side JavaScript Applications
    • Command Line tools
    • Desktop gui-based Applications
    • Hybrid applications (Titanium, Adobe AIR)

Programs written with the Commonjs API can be run in different parsers and hosting environments. Using a COMMONJS-compliant system, you can use JS to write:

1. Server Segment Program

2. Command line Program

3. Desktop GUI Program

4. Mixing program

http://arstechnica.com/business/2009/12/commonjs-effort-sets-javascript-on-path-for-world-domination/

CommonJS is a growing collection of standards, including

  • Modules
  • Binary Strings and Buffers
  • Charset Encodings
  • Binary, buffered, and textual input and output (IO) streams
  • System process arguments, environment, and streams
  • File System Interface
  • Socket streams
  • Unit test assertions, running, and reporting
  • Web Server Gateway Interface, JSGI
  • Local and remote packages and package management

So far, CommonJS have converged on specifications for modules (1.1), a System module (1.0), and a unit testing API (1.0).

Module Specification:

http://wiki.commonjs.org/wiki/Modules/1.1.1

Math.js
exports. add = function ({var sum = 0, I = 0= arguments= args. length (i < l{sum += args[i++] return sum;}                
Increment.js
= Require(' math ').  Add; exports.  Function(valreturn add(val1);} ;
Program.js
= Require(' increment ').  Increment;  1; Inc(a)//2 module." Program ";           

Http://www.ruanyifeng.com/blog/2015/05/commonjs-in-browser.html

Browser-side Emulation library

Https://github.com/efacilitation/commonjs-require

Browser-side CommonJS require () function.

<HTML><Head> <Scriptsrc= "Commonjs-require.js"></Script><Script>Require.register ("Module", function(exports, require, module) {/*Some Module Content*/Module.exports= "content"}) Console.log (Require ('Module'))</Script>        <style>        </style></Head> <Body>        <H1>Hello world!</H1></Body></HTML>

LOG:

content

CommonJS Browser Implementation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.