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
- 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 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:
commonJS 浏览器实现的更多相关文章
- commonJS — 浏览器操作(for Browser)
for Browser github: https://github.com/laixiangran/commonJS/blob/master/src/forBrowser.js 代码 /** * C ...
- js-模块化(三大模块化规范)
###1. JS模块化 * 模块化的理解 * 什么是模块? * 将一个复杂的程序依据一定的规则(规范)封装成几个块(文件), 并进行组合在一起 * 块的内部数据/实现是私有的, 只是向外部 ...
- node+pm2+express+mysql+sequelize来搭建网站和写接口
前面的话:在这里已经提到了安装node的方法,node是自带npm的.我在技术中会用es6去编写,然后下面会分别介绍node.pm2.express.mysql.sequelize.有少部分是摘抄大佬 ...
- JS模块化规范CMD之SeaJS
1. 在接触规范之前,我们用模块化来封装代码大多为如下: ;(function (形参模块名, 依赖项, 依赖项) { // 通过 形参模块名 修改模块 window.模块名 = 形参模块名 })(w ...
- 使用ECMAScript 6 模块封装代码
JavaScript 用"共享一切"的方法加载代码,这是该语言中最容易出错且最容易让人感到困惑的地方.其他语言使用诸如包这样的概念来定义代码作用域,但在 ECMAScript 6 ...
- webpack学习2.1 模块化开发(JS模块化&CSS模块化)
一.JS模块化 命名空间,COMMONJS,AMD/CMD/UMD,ES6 module 1.什么是命名空间 库名.类别名.方法名 弊端:在命名空间重复生命,要记住完整的路径名(而且很长) var N ...
- 为什么commonjs不适合于浏览器端
有了服务器端模块以后,很自然地,大家就想要客户端模块.而且最好两者能够兼容,一个模块不用修改,在服务器和浏览器都可以运行. 但是,由于一个重大的局限,使得CommonJS规范不适用于浏览器环境.还是上 ...
- "浏览器端" 使用 commonjs 模块规范开发网页应用,像开发 node 那样开发网页应用
Containjs 1.0 Containjs 是什么? Containjs 是一个基于 Commonjs 模块管理规范的 浏览器端 的 JavaScript 模块加载器(目前为非标准的,代码会持续迭 ...
- 使用Browserify来实现CommonJS的浏览器加载
前面的话 Nodejs的模块是基于CommonJS规范实现的,可不可以应用在浏览器环境中呢? var math = require('math'); math.add(2, 3); 第二行math.a ...
随机推荐
- IBatis.Net使用总结(三)-- IBatis实现分页返回数据和总数
IBatis 分页,这里没有使用其他插件,只使用最原始的方法. 输入参数: int currentPage 当前页 int pageSize 每页大小 Hashtable findCondition ...
- HDU 2509 Be the Winner nim博弈变形
Be the Winner Problem Description Let's consider m apples divided into n groups. Each group contai ...
- 初学微信小程序
最近微信推出了微信小程序,为此我学了几天,基本了解了组件及简单语法,但是今天我自己想要独立写一个demo时,忽然发现难道我的不是微信小程序的语法(我以前是iOS 开发,不用css),而是css样式的设 ...
- CodeForces 13E 分块
题目链接:http://codeforces.com/problemset/problem/13/E 题意:给定n个弹簧和每个弹簧初始的弹力a[].当球落在第i个位置.则球会被弹到i+a[i]的位置. ...
- XE2 泛型练习1
要引用单元 System.Generics.Collections implementation {$R *.dfm}var i: Integer; str: string; procedure TF ...
- 【DP】HDU 1087
HDU 1078 Super Jumping! Jumping! Jumping! 题意: 有这么个游戏,从start到end(自己决定在哪停下来)连续跳圈,中间不能空一个圈不跳,圈里的数字必须比你上 ...
- IIS运行.NET4.0配置
IIS运行.NET4.0配置 “/CRM”应用程序中的服务器错误.配置错误说明: 在处理向该请求提供服务所需的配置文件时出错.请检查下面的特定错误详细信息并适当地修改配置文件. 分析器错误消息: 无法 ...
- 用Struts2拦截器实现文件下载前的验证
思想:用户登录后,将登录信息存储在session中,每次需要验证时,取出来验证 缺陷:没有实现多用户登录时的情况 实行步骤: 登录信息的存储: ActionContext actionContext ...
- Javascript初学篇章_4(循环与函数)
七.循环语句 1.While 语法: while (exp){ //statements; } 说明: while (变量<=结束值){ 需执行的代码 } 例: var i=0; while(i ...
- linq To DataTable
//将IEnumerable<T>类型的集合转换为DataTable类型 1111 public static DataTable LINQToDataTable<T>(IEn ...