commonJS 瀏覽器實現

commonjs

http://www.commonjs.org/javascript

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 are useful for building browser-based applications. However, the spec does not define a standard library that is useful for building a broader range of applications.html

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. The intention is that an application developer will be able to write an application using the CommonJS APIs and then run that application across different JavaScript interpreters and host environments. With CommonJS-compliant systems, you can use JavaScript to write:java

  • Server-side JavaScript applications
  • Command line tools
  • Desktop GUI-based applications
  • Hybrid applications (Titanium, Adobe AIR)

使用知足commonjs API書寫的程序, 能夠運行在不一樣的解析器和宿主環境中。 使用符合commonjs的系統, 你可使用js寫:git

一、 服務器段程序github

二、 命令行程序瀏覽器

三、 桌面GUI程序服務器

四、 混合程序app

 

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

CommonJS is a growing collection of standards, includingide

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

 

模塊規範:

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

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   module.id == "program";

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

瀏覽器端模擬庫

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

browser-side CommonJS require() function.

 

 

<html>
<head> 
<script src="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
相關文章
相關標籤/搜索