From 79827f4c0be2555050a65ed63006de03354f7179 Mon Sep 17 00:00:00 2001 From: Hector Correa Date: Mon, 4 Jun 2012 21:31:43 -0400 Subject: [PATCH] switched tabs to spaces --- 03_function.coffee | 2 +- 07_existential.coffee | 6 +- 07_existential.js | 10 +-- 07_existentialchain.coffee | 12 +-- 07_existentialchain.js | 6 +- 10_context.coffee | 2 +- 10_context.js | 5 +- 20_class.coffee | 10 +-- 21_class_def_args_and_this.coffee | 6 +- 23_class_static_methods.coffee | 10 +-- 24_class_inheritance.coffee | 10 +-- 25_class_context.coffee | 20 ++--- 25_class_context.js | 5 +- 31_destructuring_object.coffee | 12 +-- 32_loops.coffee | 6 +- 33_loops_jquery.coffee | 14 ++-- js_primer/closure_fade.html | 24 +++--- js_primer/func.html | 48 ++++++------ js_primer/settimeout.html | 120 +++++++++++++++--------------- node/helloworld_webserver.coffee | 4 +- 20 files changed, 165 insertions(+), 167 deletions(-) diff --git a/03_function.coffee b/03_function.coffee index 1944ea4..59d424f 100644 --- a/03_function.coffee +++ b/03_function.coffee @@ -1,4 +1,4 @@ -square = (x) -> +square = (x) -> x * x n = 9 diff --git a/07_existential.coffee b/07_existential.coffee index 8ae0ed5..b61c361 100644 --- a/07_existential.coffee +++ b/07_existential.coffee @@ -2,9 +2,9 @@ billTo = {} billTo.street = "111 Accounting St." billTo.city = "State College" -shipTo = {} -shipTo.street = "222 Warehouse Blvd." -shipTo.city = "Bellefonte" +# shipTo = {} +# shipTo.street = "222 Warehouse Blvd." +# shipTo.city = "Bellefonte" label = shipTo ? billTo console.log "#{label.street}" diff --git a/07_existential.js b/07_existential.js index a91d17c..1472316 100644 --- a/07_existential.js +++ b/07_existential.js @@ -1,6 +1,6 @@ // Generated by CoffeeScript 1.3.1 (function() { - var billTo, label, shipTo; + var billTo, label; billTo = {}; @@ -8,13 +8,7 @@ billTo.city = "State College"; - shipTo = {}; - - shipTo.street = "222 Warehouse Blvd."; - - shipTo.city = "Bellefonte"; - - label = shipTo != null ? shipTo : billTo; + label = typeof shipTo !== "undefined" && shipTo !== null ? shipTo : billTo; console.log("" + label.street); diff --git a/07_existentialchain.coffee b/07_existentialchain.coffee index 78ee44a..3e0adc9 100644 --- a/07_existentialchain.coffee +++ b/07_existentialchain.coffee @@ -6,12 +6,12 @@ shipTo = {} shipTo.street = "222 Warehouse Bldv." shipTo.city = "Bellefonte" shipTo.attn = - name: "John Doe" - title: "Mr." + name: "John Doe" + title: "Mr." -city = shipTo?.city ? billTo.city -console.log "City: #{city}" +# city = shipTo?.city ? billTo.city +# console.log "City: #{city}" -# attn = shipTo?.attn?.name ? "N/A" -# console.log "Attn: #{attn}" +attn = shipTo?.attn?.name ? "N/A" +console.log "Attn: #{attn}" diff --git a/07_existentialchain.js b/07_existentialchain.js index 5f17244..eb70936 100644 --- a/07_existentialchain.js +++ b/07_existentialchain.js @@ -1,6 +1,6 @@ // Generated by CoffeeScript 1.3.1 (function() { - var billTo, city, shipTo, _ref; + var attn, billTo, shipTo, _ref, _ref1; billTo = {}; @@ -19,8 +19,8 @@ title: "Mr." }; - city = (_ref = shipTo != null ? shipTo.city : void 0) != null ? _ref : billTo.city; + attn = (_ref = shipTo != null ? (_ref1 = shipTo.attn) != null ? _ref1.name : void 0 : void 0) != null ? _ref : "N/A"; - console.log("City: " + city); + console.log("Attn: " + attn); }).call(this); diff --git a/10_context.coffee b/10_context.coffee index 93d46f1..6b913da 100644 --- a/10_context.coffee +++ b/10_context.coffee @@ -7,7 +7,7 @@ $ -> # Value of "this" if the jQuery XML HTTP Request (jqXHR) object $('#btnTest2').click -> url = "file:///Users/hector/dev/coffee/intro-to-coffeescript/10_context.txt" - $.get url, (data) -> + $.get url, (data) => alert this.value # # Value of "this" is the DOM Window diff --git a/10_context.js b/10_context.js index 9c4ab47..5c5b22a 100644 --- a/10_context.js +++ b/10_context.js @@ -6,10 +6,11 @@ return alert(this.value); }); $('#btnTest2').click(function() { - var url; + var url, + _this = this; url = "file:///Users/hector/dev/coffee/intro-to-coffeescript/10_context.txt"; return $.get(url, function(data) { - return alert(this.value); + return alert(_this.value); }); }); }); diff --git a/20_class.coffee b/20_class.coffee index 39d91a9..78bd4e1 100644 --- a/20_class.coffee +++ b/20_class.coffee @@ -1,12 +1,12 @@ class TaxCalculator - tax = 0 + tax = 0 - constructor: (tax) -> - this.tax = tax + constructor: (tax) -> + this.tax = tax - taxAmount: (price) -> price * (this.tax/100) + taxAmount: (price) -> price * (this.tax/100) - totalPrice: (price) -> price + (price * (this.tax/100)) + totalPrice: (price) -> price + (price * (this.tax/100)) calc = new TaxCalculator(20) diff --git a/21_class_def_args_and_this.coffee b/21_class_def_args_and_this.coffee index da09eda..b9f31b1 100644 --- a/21_class_def_args_and_this.coffee +++ b/21_class_def_args_and_this.coffee @@ -1,10 +1,10 @@ class TaxCalculator - constructor: (@tax = 10) -> + constructor: (@tax = 10) -> - taxAmount: (price) -> price * (@tax/100) + taxAmount: (price) -> price * (@tax/100) - totalPrice: (price) -> price + @taxAmount(price) + totalPrice: (price) -> price + @taxAmount(price) calc = new TaxCalculator(30) diff --git a/23_class_static_methods.coffee b/23_class_static_methods.coffee index d2aec26..7b3984d 100644 --- a/23_class_static_methods.coffee +++ b/23_class_static_methods.coffee @@ -1,13 +1,13 @@ class TaxCalculator - constructor: (@tax = 10) -> + constructor: (@tax = 10) -> - taxAmount: (price) -> price * (@tax/100) + taxAmount: (price) -> price * (@tax/100) - totalPrice: (price) -> price + @taxAmount(price) + totalPrice: (price) -> price + @taxAmount(price) - @isTaxableState: (state) -> - state in ['PA', 'MO', 'NY'] + @isTaxableState: (state) -> + state in ['PA', 'MO', 'NY'] state = 'NY' isTaxable = TaxCalculator.isTaxableState(state) diff --git a/24_class_inheritance.coffee b/24_class_inheritance.coffee index 07ed516..b1a1b94 100644 --- a/24_class_inheritance.coffee +++ b/24_class_inheritance.coffee @@ -1,17 +1,17 @@ class TaxCalculator - constructor: (@tax = 10) -> + constructor: (@tax = 10) -> - taxAmount: (price) -> price * (@tax/100) + taxAmount: (price) -> price * (@tax/100) - totalPrice: (price) -> price + @taxAmount(price) + totalPrice: (price) -> price + @taxAmount(price) class MeanTaxCalculator extends TaxCalculator - totalPrice: (price) -> price + @taxAmount(price) + 25 + totalPrice: (price) -> price + @taxAmount(price) + 25 - anotherMethod: -> "hello" + anotherMethod: -> "hello" calc = new MeanTaxCalculator() total = calc.totalPrice(500) diff --git a/25_class_context.coffee b/25_class_context.coffee index 19f2cf2..75f99c7 100644 --- a/25_class_context.coffee +++ b/25_class_context.coffee @@ -1,18 +1,18 @@ class InvoiceForm - constructor: (@customer) -> + constructor: (@customer) -> - # Value of "this" might not be the InvoiceForm class - calculateTotal: -> - # do some fancy calculation - total = 10 - alert "#{this.customer} your total is #{total}" + # Value of "this" might not be the InvoiceForm class + calculateTotal: => + # do some fancy calculation + total = 10 + alert "#{this.customer} your total is #{total}" $ -> - invoice = new InvoiceForm("Acme Corporation") - invoice.calculateTotal() - - $('#btnCalculate').click invoice.calculateTotal + invoice = new InvoiceForm("Acme Corporation") + invoice.calculateTotal() + + $('#btnCalculate').click invoice.calculateTotal # If you look at the JavaScript code generated, the diff --git a/25_class_context.js b/25_class_context.js index f780e8b..1ed993a 100644 --- a/25_class_context.js +++ b/25_class_context.js @@ -1,6 +1,7 @@ // Generated by CoffeeScript 1.3.1 (function() { - var InvoiceForm; + var InvoiceForm, + __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; }; InvoiceForm = (function() { @@ -8,6 +9,8 @@ function InvoiceForm(customer) { this.customer = customer; + this.calculateTotal = __bind(this.calculateTotal, this); + } InvoiceForm.prototype.calculateTotal = function() { diff --git a/31_destructuring_object.coffee b/31_destructuring_object.coffee index 8f3a376..a667e8e 100644 --- a/31_destructuring_object.coffee +++ b/31_destructuring_object.coffee @@ -3,12 +3,12 @@ fetchCustomer = (id) -> # Make an Ajax request to fetch the customer data... - customer = - name: "Acme Corporation" - address: "123 desert" - attention: "Wile E. Coyote" - email: "wile@acme.com" - customer + customer = + name: "Acme Corporation" + address: "123 desert" + attention: "Wile E. Coyote" + email: "wile@acme.com" + customer {name, address} = fetchCustomer(1) console.log "#{name}: #{address}" \ No newline at end of file diff --git a/32_loops.coffee b/32_loops.coffee index 9eef6e7..9f3b8a8 100644 --- a/32_loops.coffee +++ b/32_loops.coffee @@ -10,16 +10,16 @@ colors = ['red', 'blue', 'white'] console.log "Sample 1 (loop) ------------" for color in colors - console.log color + console.log color console.log "\r\nSample 2 (loop with i) ------------" for color, i in colors - console.log i, color + console.log i, color console.log "\r\nSample 3 (comprehension) ------------" -# toUpper = (value) -> value.toUpperCase() +#toUpper = (value) -> value.toUpperCase() upper = (c.toUpperCase() for c in colors) console.log upper diff --git a/33_loops_jquery.coffee b/33_loops_jquery.coffee index d5ccc00..ac173d0 100644 --- a/33_loops_jquery.coffee +++ b/33_loops_jquery.coffee @@ -1,10 +1,10 @@ $ -> - $("#btnSave").click -> - for r in $(".required") - if $(r).val() is "" - $(r).addClass("missing") - else - $(r).removeClass("missing") + $("#btnSave").click -> + for r in $(".required") + if $(r).val() is "" + $(r).addClass("missing") + else + $(r).removeClass("missing") - return \ No newline at end of file + return \ No newline at end of file diff --git a/js_primer/closure_fade.html b/js_primer/closure_fade.html index e3e6314..2326b73 100644 --- a/js_primer/closure_fade.html +++ b/js_primer/closure_fade.html @@ -12,23 +12,23 @@ by Douglas Crockford p. 38 --> - +

Fade Color Example

Count:

diff --git a/js_primer/settimeout.html b/js_primer/settimeout.html index 2c7f5f4..a50cafb 100644 --- a/js_primer/settimeout.html +++ b/js_primer/settimeout.html @@ -1,69 +1,69 @@ -

setTimeOut

-

- - + sampleThree(); + diff --git a/node/helloworld_webserver.coffee b/node/helloworld_webserver.coffee index 2b1e38f..46c000b 100644 --- a/node/helloworld_webserver.coffee +++ b/node/helloworld_webserver.coffee @@ -2,8 +2,8 @@ # http://geekiriki.blogspot.com/2010/07/getting-started-with-nodejs-and.html # server = require('http').createServer (request, response) -> - response.writeHead 200, {'Content-Type': 'text/plain'} - response.end 'Hello World from CoffeeScript (and node.js)' + response.writeHead 200, {'Content-Type': 'text/plain'} + response.end 'Hello World from CoffeeScript (and node.js)' port = 8124 server.listen port