Closure

Closure The Definitive Guide

quick tutorial

http://lahosken.san-francisco.ca.us/frivolity/prog/closure_ffdemo/

different ways to enclose js

https://developers.google.com/closure/library/docs/gettingstarted

if your project is simple, doesn't need your own js file.

<script src="closure-library/closure/goog/base.js"></script>
<script>
  goog.require('goog.dom');
</script>
<script>
  var newHeader = goog.dom.createDom('h1');
</script>

basically, the goog.require(…) will the following statement to be added to the document automatically

<script src="closure-library/closure/goog/dom/dom.js"></script>

directly include your project js file in html.

<html>
  <head>
    <script src="closure-library/closure/goog/base.js"></script>
    <script src="hello.js"></script>
  </head>
  <body onload="sayHi()">
  </body>
</html>
bundle all js into one compiled file by closurebuilder.py

thus you don't need to include both base.js and hello.js

<html>
  <head>
    <script src="start-compiled.js"></script>
  </head>
  <body>
    <script>
      myproject.start();
    </script>
  </body>
</html>

use dependence approach depswriter

- you will need include two files, one is base.js, the other is myproject-deps.js;
- and you also need use goog.require in html so the actual js file can be inserted implicitly, because myproject-deps.js only defines where to fetch those those dependent files.

<html>
  <head>
    <script src="/closure/goog/base.js"></script>
    <script src="/myproject/myproject-deps.js"></script>
    <script>
      goog.require('myproject.foo');
    </script>
  </head>
  <body>
    <!-- content -->
    <script>
      myproject.foo.start();
    </script>
  </body>
</html>

turn on off debug

http://scriptble.com/2012/02/01/removing-debug-statements-from-javascript-via-compiler/

if (goog.DEBUG) {
  ...
}

java -jar compiler.jar —js input.js —define=DEBUG=false —js_output_file output.js —compilation_level SIMPLE_OPTIMIZATIONS —formatting PRETTY_PRINT
[[/code]]

annotations

http://google-styleguide.googlecode.com/svn/trunk/javascriptguide.xml?showone=Comments#Comments
https://developers.google.com/closure/compiler/docs/js-for-compiler

export vs external

  • us external 3rd party js function by declaring externs:

https://developers.google.com/closure/compiler/docs/api-tutorial3#externs

  • all others to use our js function: @export

http://cvmlrobotics.blogspot.com/2012/08/use-object-instance-function-as-jsonp.html

https://code.google.com/p/closure-compiler/wiki/FAQ#How_do_I_write_an_externs_file?

http://sontek.net/blog/detail/using-django-context-variables-with-javascriptajax

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License