在学习limejs的时候,看到了limejs中就使用到了closure库。非常喜欢它的类定义方式和JS压缩方法。记录下来希望在以后的项目中能够使用上。
官方文档参考:https://developers.google.com/closure/library/
以下是官方文档对closure库的介绍
The Closure Library is a broad, well-tested, modular, and cross-browser JavaScript library. You can pull just what you need from a large set of reusable UI widgets and controls, and from lower-level utilities for DOM manipulation, server communication, animation, data structures, unit testing, rich-text editing, and more.
If you are developing a large or growing application, you may benefit from the Closure Library's breadth. A well-tested library can insulate you from cross-browser compatibility issues and the minutiae of client-side programming, letting you focus on the fun stuff.
closure库是谷歌开发的一个广泛的、通过测试的、模块化的和跨浏览器的JavaScript库。closure库提供大量的可重用的UI小部件和控件,还有一些底层的使用工具如DOM操作,服务器通信、动画、数据结构、单元测试、富文本编辑等等,方便你的使用。
如果你开发一个大型的或者是快速增长的Web应用程序,Closure是一个不错的选择。这个可以使你不必去考虑浏览器间的兼容问题和一些客户端开发的小细节,而专心于你所感兴趣的东西。
目前谷歌的一些产品都使用了Closure库。比如:谷歌搜索、Gmail、谷歌地图等。
git clone https://code.google.com/p/closure-library/
goog.require( 'goog.dom');
//引入goog.dom类在页面上绘制dom内容
mydep.writer.sayHello = function(){
var newDiv = goog.dom.createDom( 'h1', { 'style' : 'background-color:#EEE'},
'Hello world!');
goog.dom.appendChild(document.body, newDiv);
}
goog.exportSymbol( 'mydep.writer', mydep.writer);
goog.require( 'mydep.writer');
mydep.start = function() {
mydep.writer.sayHello();
}
goog.exportSymbol( 'mydep.start', mydep.start);
<head >
<script src = "../closure-library/closure/goog/base.js" > < /script >
<script src = "mydep.js" > < /script >
< /head >
<body onload = "mydep.start()" >
< /body >
< /html >
- --output_file:指定一个输出文件路径。如果没有指定输出路径。只是在控制台打印信息。
- --root 扫描root目录下的js文件。这个root路径相对于生成的deps文件路径。框架默认的deps.js文件在closure-library\closure\goog下。
- --root_with_prefix 同样是定义扫描的文件夹但是在输出路径的时候加上一个前缀。如定义:--root_with_prefix="dep ../../../dep"输出的时候就变成了:
- --path_with_depspath 替换指定文件的依赖关系到某个文件中。如使用:--path_with_depspath="dep/mydep.js ../test.js" 则输出: