|
Hate UML?Draw sequence diagrams in seconds.http://www.websequencediagrams.com |
class Animal
constructor: (@sound) ->
makeSound: ->
alert @sound
class Cow extends Animal
constructor: ->
super "moo"
###* @param {number} numHours ###
graze: (numHours) ->
animal = new Cow()
animal.makeSound()
animal.graze "16"
animal.eatGrass()
There are some errors in this file that wouldn't normally be detected until you run it.
Let's run jzbuild.py on it (Download jzbuild.py). It works on Linux or Windows as long as you have java and python installed. You do not need anything else.
jzbuild --closure --out out.js Animal.coffee
Creating /home/smhanov/.jzbuild
/home/smhanov/.jzbuild/rhino.jar not found! Downloading from ftp://ftp.mozilla.org/pub/mozilla.org/js/rhino1_7R2.zip
Could not find makefile.jz. Running jslint on input files.
Building release...
/home/smhanov/.jzbuild/jslint-rhino.js not found. Creating.
/home/smhanov/.jzbuild/compiler.jar not found! Downloading from http://closure-compiler.googlecode.com/files/compiler-latest.zip
Running closure compiler.
Fetching http://github.com/smhanov/jzbuild/raw/master/externs/jquery-mobile.js
Fetching http://closure-compiler.googlecode.com/svn/trunk/contrib/externs/jquery-1.5.js
Fetching http://closure-compiler.googlecode.com/svn/trunk/contrib/externs/json.js
java -jar /home/smhanov/.jzbuild/compiler.jar --compilation_level SIMPLE_OPTIMIZATIONS --warning_level VERBOSE --externs /home/smhanov/.jzbuild/jquery-mobile.js --externs /home/smhanov/.jzbuild/jquery-1.5.js --externs /home/smhanov/.jzbuild/json.js --js /tmp/tmptYeoZk --js Animal.js --js /tmp/tmpk3Azo0.js
Animal.js:26: WARNING - actual parameter 1 of Cow.prototype.graze does not match formal parameter
found : string
required: number
animal.graze("16");
^
Animal.js:27: WARNING - Property eatGrass never defined on Cow
animal.eatGrass();
^
0 error(s), 2 warning(s), 72.4% typed
Here's what it gets compiled to, in out.js:
var __hasProp=Object.prototype.hasOwnProperty,__extends=function(b,a){function c(){this.constructor=b}for(
var d in a)__hasProp.call(a,d)&&(b[d]=a[d]);c.prototype=a.prototype;b.prototype=new
c;b.__super__=a.prototype;return b},__bind=function(b,a){return function(){
return b.apply(a,arguments)}},__indexOf=Array.prototype.indexOf||function(b){for(
var a=0,c=this.length;a<c;a++)if(this[a]===b)return a;return-1},__slice=Array.prototype.slice;
var animal,Animal=function(b){this.sound=b};Animal.prototype.makeSound=function(){return alert(this.sound)};
var Cow=function(){Cow.__super__.constructor.call(this,"moo")};__extends(Cow,Animal);Cow.prototype.graze=function(){};
animal=new Cow;animal.makeSound();animal.graze("16");animal.eatGrass();
We can further minify and optimize the code by specifying the --release option.
jzbuild --closure --release --out out.js Animal.coffee
var c=Object.prototype.hasOwnProperty;var d;function g(){g.a.constructor.call(this,"moo")}(function(a,b){
function f(){this.constructor=a}for(var e in b)c.call(b,e)&&(a[e]=b[e]);f.prototype=b.prototype;a.prototype=
new f;a.a=b.prototype;return a})(g,function(a){this.b=a});d=new g;alert(d.b);d.c();
I recently discovered Coffeescript, and it is a real time saver. Writing a file in Coffeescript takes only 2/3 of the lines of code. I can physically write code faster, and I can see 50% more of my code on the screen at a time. So naturally I wanted to update jzbuild to take coffeescript as well as .js files as input.
Jzbuild is nothing but a glorified installer for other programs. It is possible due to the work of many others, with special mentions for:
Want more programming tech talk?
Add to Circles on Google Plus
Subscribe to posts
Post comment