From 834442148bae7dd6816868d8377bf4445c6b7102 Mon Sep 17 00:00:00 2001
From: Jeremy Ashkenas
+ Declarations of new variables are pushed up to the top of the current scope, + so that assignments may always be used within expressions. +
Objects and Arrays @@ -293,7 +297,12 @@ coffee-script --print app/scripts/*.coffee > concatenation.js
The same mechanism is used to push down assignment through switch statements, and if-elses (although the ternary operator is preferred). + Another part of manipulating assignment statements is + CoffeeScript's declaration of new variables at the top of the + current scope. This allows assignment to be used as a piece of an + expression.
+ <%= code_for('expressions_assignment', 'six') %>Aliases diff --git a/documentation/js/aliases.js b/documentation/js/aliases.js index 804d56ea..c033ee2e 100644 --- a/documentation/js/aliases.js +++ b/documentation/js/aliases.js @@ -1,8 +1,8 @@ (function(){ + var volume; if (ignition === true) { launch(); } - var volume; if (band !== spinal_tap) { volume = 10; } diff --git a/documentation/js/array_comprehensions.js b/documentation/js/array_comprehensions.js index 790281c9..636ccb49 100644 --- a/documentation/js/array_comprehensions.js +++ b/documentation/js/array_comprehensions.js @@ -1,20 +1,19 @@ (function(){ - + var __a, __b, __c, __d, __e, __f, __g, __h, food, i, lunch, row; // Eat lunch. - var lunch; - var __a = ['toast', 'cheese', 'wine']; - var __d = []; - for (var __b=0, __c=__a.length; __b<__c; __b++) { - var food = __a[__b]; + __a = ['toast', 'cheese', 'wine']; + __d = []; + for (__b=0, __c=__a.length; __b<__c; __b++) { + food = __a[__b]; __d[__b] = food.eat(); } lunch = __d; // Zebra-stripe a table. - var __e = table; - var __h = []; - for (var __f=0, __g=__e.length; __f<__g; __f++) { - var row = __e[__f]; - var i = __f; + __e = table; + __h = []; + for (__f=0, __g=__e.length; __f<__g; __f++) { + row = __e[__f]; + i = __f; __h[__f] = i % 2 === 0 ? highlight(row) : null; } __h; diff --git a/documentation/js/assignment.js b/documentation/js/assignment.js index 0a586871..69bd8eb2 100644 --- a/documentation/js/assignment.js +++ b/documentation/js/assignment.js @@ -1,4 +1,5 @@ (function(){ - var greeting = "Hello CoffeeScript"; - var difficulty = 0.5; + var difficulty, greeting; + greeting = "Hello CoffeeScript"; + difficulty = 0.5; })(); \ No newline at end of file diff --git a/documentation/js/conditionals.js b/documentation/js/conditionals.js index b501b7e7..b5d12fe3 100644 --- a/documentation/js/conditionals.js +++ b/documentation/js/conditionals.js @@ -1,5 +1,5 @@ (function(){ - var mood; + var date, mood; if (singing) { mood = greatly_improved; } @@ -7,6 +7,6 @@ claps_hands(); cha_cha_cha(); } - var date = friday ? sue : jill; + date = friday ? sue : jill; expensive = expensive || do_the_math(); })(); \ No newline at end of file diff --git a/documentation/js/embedded.js b/documentation/js/embedded.js index 3c16b185..d097f260 100644 --- a/documentation/js/embedded.js +++ b/documentation/js/embedded.js @@ -1,5 +1,6 @@ (function(){ - var hi = function() { + var hi; + hi = function() { return [document.title, "Hello JavaScript"].join(": "); }; })(); \ No newline at end of file diff --git a/documentation/js/expressions.js b/documentation/js/expressions.js index 9301da9c..78f9244c 100644 --- a/documentation/js/expressions.js +++ b/documentation/js/expressions.js @@ -1,5 +1,6 @@ (function(){ - var grade = function(student) { + var eldest, grade; + grade = function(student) { if (student.excellent_work) { return "A+"; } else if (student.okay_stuff) { @@ -8,5 +9,5 @@ return "C"; } }; - var eldest = 24 > 21 ? "Liz" : "Ike"; + eldest = 24 > 21 ? "Liz" : "Ike"; })(); \ No newline at end of file diff --git a/documentation/js/expressions_assignment.js b/documentation/js/expressions_assignment.js new file mode 100644 index 00000000..ec67227a --- /dev/null +++ b/documentation/js/expressions_assignment.js @@ -0,0 +1,4 @@ +(function(){ + var one, six, three, two; + six = (one = 1) + (two = 2) + (three = 3); +})(); \ No newline at end of file diff --git a/documentation/js/functions.js b/documentation/js/functions.js index 2fc0d2f8..d77c2c26 100644 --- a/documentation/js/functions.js +++ b/documentation/js/functions.js @@ -1,8 +1,9 @@ (function(){ - var square = function(x) { + var cube, square; + square = function(x) { return x * x; }; - var cube = function(x) { + cube = function(x) { return square(x) * x; }; })(); \ No newline at end of file diff --git a/documentation/js/objects_and_arrays.js b/documentation/js/objects_and_arrays.js index bd3276cc..cfd1011e 100644 --- a/documentation/js/objects_and_arrays.js +++ b/documentation/js/objects_and_arrays.js @@ -1,6 +1,7 @@ (function(){ - var song = ["do", "re", "mi", "fa", "so"]; - var ages = { + var ages, song; + song = ["do", "re", "mi", "fa", "so"]; + ages = { max: 10, ida: 9, tim: 11 diff --git a/documentation/js/overview.js b/documentation/js/overview.js index 0ed09490..32cc4b34 100644 --- a/documentation/js/overview.js +++ b/documentation/js/overview.js @@ -1,20 +1,20 @@ (function(){ - + var __a, __b, __c, __d, cubed_list, list, math, num, number, opposite_day, square; // Assignment: - var number = 42; - var opposite_day = true; + number = 42; + opposite_day = true; // Conditions: if (opposite_day) { number = -42; } // Functions: - var square = function(x) { + square = function(x) { return x * x; }; // Arrays: - var list = [1, 2, 3, 4, 5]; + list = [1, 2, 3, 4, 5]; // Objects: - var math = { + math = { root: Math.sqrt, square: square, cube: function(x) { @@ -22,11 +22,10 @@ } }; // Array comprehensions: - var cubed_list; - var __a = list; - var __d = []; - for (var __b=0, __c=__a.length; __b<__c; __b++) { - var num = __a[__b]; + __a = list; + __d = []; + for (__b=0, __c=__a.length; __b<__c; __b++) { + num = __a[__b]; __d[__b] = math.cube(num); } cubed_list = __d; diff --git a/documentation/js/scope.js b/documentation/js/scope.js index 1f110baa..c5735c04 100644 --- a/documentation/js/scope.js +++ b/documentation/js/scope.js @@ -1,9 +1,11 @@ (function(){ - var num = 1; - var change_numbers = function() { + var change_numbers, new_num, num; + num = 1; + change_numbers = function() { + var new_num; num = 2; - var new_num = 3; + new_num = 3; return new_num; }; - var new_num = change_numbers(); + new_num = change_numbers(); })(); \ No newline at end of file diff --git a/documentation/js/slices.js b/documentation/js/slices.js index 5f590283..d819c4ae 100644 --- a/documentation/js/slices.js +++ b/documentation/js/slices.js @@ -1,5 +1,6 @@ (function(){ - var numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; - var three_to_six = numbers.slice(3, 6 + 1); - var numbers_copy = numbers.slice(0, numbers.length); + var numbers, numbers_copy, three_to_six; + numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; + three_to_six = numbers.slice(3, 6 + 1); + numbers_copy = numbers.slice(0, numbers.length); })(); \ No newline at end of file diff --git a/documentation/js/strings.js b/documentation/js/strings.js index b3407d7f..0b486548 100644 --- a/documentation/js/strings.js +++ b/documentation/js/strings.js @@ -1,5 +1,6 @@ (function(){ - var moby_dick = "Call me Ishmael. Some years ago -- \ + var moby_dick; + moby_dick = "Call me Ishmael. Some years ago -- \ never mind how long precisely -- having little \ or no money in my purse, and nothing particular \ to interest me on shore, I thought I would sail \ diff --git a/documentation/js/super.js b/documentation/js/super.js index cfb99e5c..5f857d2f 100644 --- a/documentation/js/super.js +++ b/documentation/js/super.js @@ -1,10 +1,11 @@ (function(){ - var Animal = function() { + var Animal, Horse, Snake, sam, tom; + Animal = function() { }; Animal.prototype.move = function(meters) { return alert(this.name + " moved " + meters + "m."); }; - var Snake = function(name) { + Snake = function(name) { this.name = name; return this.name; }; @@ -15,7 +16,7 @@ alert("Slithering..."); return Snake.__superClass__.move.call(this, 5); }; - var Horse = function(name) { + Horse = function(name) { this.name = name; return this.name; }; @@ -26,8 +27,8 @@ alert("Galloping..."); return Horse.__superClass__.move.call(this, 45); }; - var sam = new Snake("Sammy the Python"); - var tom = new Horse("Tommy the Palomino"); + sam = new Snake("Sammy the Python"); + tom = new Horse("Tommy the Palomino"); sam.move(); tom.move(); })(); \ No newline at end of file diff --git a/index.html b/index.html index 437fd44d..615fd9be 100644 --- a/index.html +++ b/index.html @@ -86,22 +86,22 @@ math: { # Array comprehensions: cubed_list: math.cube(num) for num in list. -
+
var __a, __b, __c, __d, cubed_list, list, math, num, number, opposite_day, square; // Assignment: -var number = 42; -var opposite_day = true; +number = 42; +opposite_day = true; // Conditions: if (opposite_day) { number = -42; } // Functions: -var square = function(x) { +square = function(x) { return x * x; }; // Arrays: -var list = [1, 2, 3, 4, 5]; +list = [1, 2, 3, 4, 5]; // Objects: -var math = { +math = { root: Math.sqrt, square: square, cube: function(x) { @@ -109,30 +109,29 @@ cubed_list: math.cube(num) fo } }; // Array comprehensions: -var cubed_list; -var __a = list; -var __d = []; -for (var __b=0, __c=__a.length; __b<__c; __b++) { - var num = __a[__b]; +__a = list; +__d = []; +for (__b=0, __c=__a.length; __b<__c; __b++) { + num = __a[__b]; __d[__b] = math.cube(num); } cubed_list = __d; -