Stay Hungry,Stay Foolish!

commonJS 浏览器实现

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

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:

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

使用满足commonjs API书写的程序, 可以运行在不同的解析器和宿主环境中。 使用符合commonjs的系统, 你可以使用js写:

1、 服务器段程序

2、 命令行程序

3、 桌面GUI程序

4、 混合程序

 

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

CommonJS is a growing collection of standards, including

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
posted @   lightsong  阅读(537)  评论(0编辑  收藏  举报
编辑推荐:
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
阅读排行:
· 全网最简单!3分钟用满血DeepSeek R1开发一款AI智能客服,零代码轻松接入微信、公众号、小程
· .NET 10 首个预览版发布,跨平台开发与性能全面提升
· 《HelloGitHub》第 107 期
· 全程使用 AI 从 0 到 1 写了个小工具
· 从文本到图像:SSE 如何助力 AI 内容实时呈现?(Typescript篇)
千山鸟飞绝,万径人踪灭
点击右上角即可分享
微信分享提示