…rty, with an alias, for Rhino and java objects
…-- not just the first line of the heredoc
…ntaining the computed result of each iteration.
| @@ -1,7 +1,7 @@ | ||
| Gem::Specification.new do |s| | ||
| s.name = 'coffee-script' | ||
| s.version = '0.2.4' # Keep version in sync with coffee-script.rb | ||
| s.date = '2010-1-12' | ||
| s.version = '0.2.5' # Keep version in sync with coffee-script.rb | ||
| s.date = '2010-1-13' | ||
|
|
||
| s.homepage = "http://jashkenas.github.com/coffee-script/" | ||
| s.summary = "The CoffeeScript Compiler" | ||
| @@ -0,0 +1,6 @@ | ||
| Account: customer, cart => | ||
| this.customer: customer | ||
| this.cart: cart | ||
|
|
||
| $('.shopping_cart').bind('click') event ==> | ||
| this.customer.purchase(this.cart) |
| @@ -1,9 +1,10 @@ | ||
| switch day | ||
| when "Tuesday" then eat_breakfast() | ||
| when "Wednesday" then go_to_the_park() | ||
| when "Saturday" | ||
| when "Mon" then go_to_work() | ||
| when "Tue" then go_to_the_park() | ||
| when "Thu" then go_ice_fishing() | ||
| when "Fri", "Sat" | ||
| if day is bingo_day | ||
| go_to_bingo() | ||
| go_dancing() | ||
| when "Sunday" then go_to_church() | ||
| when "Sun" then go_to_church() | ||
| else go_to_work() |
| @@ -1,5 +1,8 @@ | ||
| while demand > supply | ||
| sell() | ||
| restock() | ||
| if this.studying_economics | ||
| while supply > demand then buy() | ||
| while supply < demand then sell() | ||
|
|
||
| while supply > demand then buy() | ||
| num: 6 | ||
| lyrics: while num -= 1 | ||
| num + " little monkeys, jumping on the bed. | ||
| One fell out and bumped his head." |
| @@ -51,7 +51,7 @@ | ||
|
|
||
| <p> | ||
| <b>Latest Version:</b> | ||
| <a href="http://gemcutter.org/gems/coffee-script">0.2.4</a> | ||
| <a href="http://gemcutter.org/gems/coffee-script">0.2.5</a> | ||
| </p> | ||
|
|
||
| <h2>Table of Contents</h2> | ||
| @@ -76,6 +76,7 @@ | ||
| <a href="#inheritance">Inheritance, and Calling Super from a Subclass</a><br /> | ||
| <a href="#blocks">Blocks</a><br /> | ||
| <a href="#pattern_matching">Pattern Matching</a><br /> | ||
| <a href="#long_arrow">Function Binding</a><br /> | ||
| <a href="#embedded">Embedded JavaScript</a><br /> | ||
| <a href="#switch">Switch/When/Else</a><br /> | ||
| <a href="#try">Try/Catch/Finally</a><br /> | ||
| @@ -387,9 +388,12 @@ coffee --print app/scripts/*.coffee > concatenation.js</pre> | ||
|
|
||
| <p id="while"> | ||
| <b class="header">While Loops</b> | ||
| The only low-level loop that CoffeeScript provides is the while loop. | ||
| The only low-level loop that CoffeeScript provides is the <b>while</b> loop. The | ||
| main difference from JavaScript is that the <b>while</b> loop can be used | ||
| as an expression, returning an array containing the result of each iteration | ||
| through the loop. | ||
| </p> | ||
| <%= code_for('while') %> | ||
| <%= code_for('while', 'lyrics.join("\n")') %> | ||
| <p> | ||
| Other JavaScript loops, such as <b>for</b> loops and <b>do-while</b> loops | ||
| can be mimicked by variations on <b>while</b>, but the hope is that you | ||
| @@ -528,6 +532,17 @@ coffee --print app/scripts/*.coffee > concatenation.js</pre> | ||
| </p> | ||
| <%= code_for('object_extraction', 'poet + " — " + street') %> | ||
|
|
||
| <p id="long_arrow"> | ||
| <b class="header">Function binding</b> | ||
| The long arrow <tt>==></tt> can be used to both define a function, and to bind | ||
| it to the current value of <tt>this</tt>, right on the spot. This is helpful | ||
| when using callback-based libraries like Prototype or jQuery, for creating | ||
| iterator functions to pass to <tt>each</tt>, or event-handler functions | ||
| to use with <tt>bind</tt>. Functions created with the long arrow are able to access | ||
| properties of the <tt>this</tt> where they're defined. | ||
| </p> | ||
| <%= code_for('long_arrow') %> | ||
|
|
||
| <p id="embedded"> | ||
| <b class="header">Embedded JavaScript</b> | ||
| Hopefully, you'll never need to use it, but if you ever need to intersperse | ||
| @@ -546,6 +561,11 @@ coffee --print app/scripts/*.coffee > concatenation.js</pre> | ||
| in a returnable, assignable expression. The format is: <tt>switch</tt> condition, | ||
| <tt>when</tt> clauses, <tt>else</tt> the default case. | ||
| </p> | ||
| <p> | ||
| As in Ruby, <b>switch</b> statements in CoffeeScript can take multiple | ||
| values for each <b>when</b> clause. If any of the values match, the clause | ||
| runs. | ||
| </p> | ||
| <%= code_for('switch') %> | ||
|
|
||
| <p id="try"> | ||
| @@ -624,7 +644,16 @@ coffee --print app/scripts/*.coffee > concatenation.js</pre> | ||
| </ul> | ||
|
|
||
| <h2 id="change_log">Change Log</h2> | ||
|
|
||
|
|
||
| <p> | ||
| <b class="header" style="margin-top: 20px;">0.2.5</b> | ||
| The conditions in switch statements can now take multiple values at once — | ||
| If any of them are true, the case will run. Added the long arrow <tt>==></tt>, | ||
| which defines and immediately binds a function to <tt>this</tt>. While loops can | ||
| now be used as expressions, in the same way that comprehensions can. Splats | ||
| can be used within pattern matches to soak up the rest of an array. | ||
| </p> | ||
|
|
||
| <p> | ||
| <b class="header" style="margin-top: 20px;">0.2.4</b> | ||
| Added ECMAScript Harmony style destructuring assignment, for dealing with | ||
| @@ -3,18 +3,18 @@ | ||
| // Eat lunch. | ||
| lunch = (function() { | ||
| __a = []; __b = ['toast', 'cheese', 'wine']; | ||
| for (__c=0; __c<__b.length; __c++) { | ||
| for (__c = 0; __c < __b.length; __c++) { | ||
| food = __b[__c]; | ||
| __a.push(eat(food)); | ||
| } | ||
| return __a; | ||
| })(); | ||
| // Naive collision detection. | ||
| __d = asteroids; | ||
| for (__e=0; __e<__d.length; __e++) { | ||
| for (__e = 0; __e < __d.length; __e++) { | ||
| roid = __d[__e]; | ||
| __f = asteroids; | ||
| for (__g=0; __g<__f.length; __g++) { | ||
| for (__g = 0; __g < __f.length; __g++) { | ||
| roid2 = __f[__g]; | ||
| if (roid !== roid2) { | ||
| if (roid.overlaps(roid2)) { | ||
| @@ -1,10 +1,11 @@ | ||
| (function(){ | ||
| var __a, __b, globals, name; | ||
| var __hasProp = Object.prototype.hasOwnProperty; | ||
| // The first ten global properties. | ||
| globals = ((function() { | ||
| __a = []; __b = window; | ||
| for (name in __b) { | ||
| if (__b.hasOwnProperty(name)) { | ||
| if (__hasProp.call(__b, name)) { | ||
| __a.push(name); | ||
| } | ||
| } | ||
| @@ -0,0 +1,20 @@ | ||
| (function(){ | ||
| var Account; | ||
| Account = function Account(customer, cart) { | ||
| var __a, __b; | ||
| var __this = this; | ||
| this.customer = customer; | ||
| this.cart = cart; | ||
| __a = $('.shopping_cart').bind('click', (function() { | ||
| __b = function(event) { | ||
| var __c; | ||
| __c = this.customer.purchase(this.cart); | ||
| return Account === this.constructor ? this : __c; | ||
| }; | ||
| return (function() { | ||
| return __b.apply(__this, arguments); | ||
| }); | ||
| })()); | ||
| return Account === this.constructor ? this : __a; | ||
| }; | ||
| })(); |
| @@ -1,5 +1,6 @@ | ||
| (function(){ | ||
| var __a, __b, age, ages, child, years_old; | ||
| var __hasProp = Object.prototype.hasOwnProperty; | ||
| years_old = { | ||
| max: 10, | ||
| ida: 9, | ||
| @@ -9,7 +10,7 @@ | ||
| __a = []; __b = years_old; | ||
| for (child in __b) { | ||
| age = __b[child]; | ||
| if (__b.hasOwnProperty(child)) { | ||
| if (__hasProp.call(__b, child)) { | ||
| __a.push(child + " is " + age); | ||
| } | ||
| } | ||
| @@ -34,7 +34,7 @@ | ||
| // Array comprehensions: | ||
| cubed_list = (function() { | ||
| __a = []; __b = list; | ||
| for (__c=0; __c<__b.length; __c++) { | ||
| for (__c = 0; __c < __b.length; __c++) { | ||
| num = __b[__c]; | ||
| __a.push(math.cube(num)); | ||
| } | ||
| @@ -1,14 +1,16 @@ | ||
| (function(){ | ||
| if (day === "Tuesday") { | ||
| eat_breakfast(); | ||
| } else if (day === "Wednesday") { | ||
| if (day === "Mon") { | ||
| go_to_work(); | ||
| } else if (day === "Tue") { | ||
| go_to_the_park(); | ||
| } else if (day === "Saturday") { | ||
| } else if (day === "Thu") { | ||
| go_ice_fishing(); | ||
| } else if (day === "Fri" || day === "Sat") { | ||
| if (day === bingo_day) { | ||
| go_to_bingo(); | ||
| go_dancing(); | ||
| } | ||
| } else if (day === "Sunday") { | ||
| } else if (day === "Sun") { | ||
| go_to_church(); | ||
| } else { | ||
| go_to_work(); | ||
| @@ -1,9 +1,20 @@ | ||
| (function(){ | ||
| while (demand > supply) { | ||
| sell(); | ||
| restock(); | ||
| } | ||
| while (supply > demand) { | ||
| buy(); | ||
| var __a, lyrics, num; | ||
| if (this.studying_economics) { | ||
| while (supply > demand) { | ||
| buy(); | ||
| } | ||
| while (supply < demand) { | ||
| sell(); | ||
| } | ||
| } | ||
| num = 6; | ||
| lyrics = (function() { | ||
| __a = []; | ||
| while (num -= 1) { | ||
| __a.push(num + " little monkeys, jumping on the bed. \ | ||
| One fell out and bumped his head."); | ||
| } | ||
| return __a; | ||
| })(); | ||
| })(); |
| @@ -14,63 +14,39 @@ <h1>Quickie CoffeeScript Speed Tests</h1> | ||
| var arr = []; | ||
| while (num--) arr.push(num); | ||
|
|
||
| JSLitmus.test('current comprehensions', function() { | ||
| __a = arr; | ||
| __c = []; | ||
| for (__b in __a) { | ||
| if (__a.hasOwnProperty(__b)) { | ||
| num = __a[__b]; | ||
| __d = num; | ||
| __c.push(num); | ||
| } | ||
| } | ||
| }); | ||
| var f1 = function f1() { | ||
| return arr; | ||
| }; | ||
|
|
||
| JSLitmus.test('raw for loop (best we can do)', function() { | ||
| __a = arr; | ||
| __c = new Array(__a.length); | ||
| for (__b=0; __b < __a.length; __b++) { | ||
| __c[__b] = __a[__b]; | ||
| } | ||
| JSLitmus.test('regular function', function() { | ||
| f1(); | ||
| }); | ||
|
|
||
| JSLitmus.test('current without hasOwnProperty check', function() { | ||
| __a = arr; | ||
| __c = []; | ||
| for (__b in __a) { | ||
| num = __a[__b]; | ||
| __d = num; | ||
| __c.push(num); | ||
| } | ||
| }); | ||
| var __this = this; | ||
|
|
||
| var f2 = function f2() { | ||
| return (function() { | ||
| return arr; | ||
| }).apply(__this, arguments); | ||
| }; | ||
|
|
||
| JSLitmus.test('raw for..in loop', function() { | ||
| __a = arr; | ||
| __c = new Array(__a.length); | ||
| for (__b in __a) { | ||
| __c[__b] = __a[__b]; | ||
| } | ||
| JSLitmus.test('bound function', function() { | ||
| f2(); | ||
| }); | ||
|
|
||
| JSLitmus.test('weepy\'s comprehensions', function() { | ||
| __c = []; __a = arr; | ||
| __d = function(num, __b) { | ||
| __c.push(num); | ||
| var f3 = (function() { | ||
| __b = function() { | ||
| return arr; | ||
| }; | ||
| if (__a instanceof Array) { | ||
| for (__b=0; __b<__a.length; __b++) __d(__a[__b], __b); | ||
| } else { | ||
| for (__b in __a) { if (__a.hasOwnProperty(__b)) __d(__a[__b], __b); } | ||
| } | ||
| }); | ||
| return (function f2() { | ||
| return __b.apply(__this, arguments); | ||
| }); | ||
| })(); | ||
|
|
||
| JSLitmus.test('CoffeeScript 0.2.2 comprehensions', function() { | ||
| __c = []; __a = arr; | ||
| for (__b=0; __b<__a.length; __b++) { | ||
| num = __a[__b]; | ||
| __c.push(num); | ||
| } | ||
| JSLitmus.test('prebound function', function() { | ||
| f3(); | ||
| }); | ||
|
|
||
| </script> | ||
|
|
||
| </body> | ||
| @@ -15,8 +15,8 @@ dc.model.Document: dc.Model.extend({ | ||
| # document by binding to Metadata, instead of on-the-fly. | ||
| metadata: => | ||
| docId: this.id | ||
| _.select(Metadata.models(), (meta => | ||
| _.any(meta.get('instances'), instance => | ||
| _.select(Metadata.models(), (meta => | ||
| _.any(meta.get('instances'), instance => | ||
| instance.document_id is docId))) | ||
|
|
||
| bookmark: pageNumber => | ||
| @@ -60,7 +60,7 @@ dc.model.DocumentSet: dc.model.RESTfulSet.extend({ | ||
| # change their selected state. | ||
| _onModelEvent: e, model => | ||
| this.base(e, model) | ||
| fire: e == dc.Model.CHANGED and model.hasChanged('selected') | ||
| fire: e is dc.Model.CHANGED and model.hasChanged('selected') | ||
| if fire then _.defer(_(this.fire).bind(this, this.SELECTION_CHANGED, this)) | ||
|
|
||
| }) | ||