From 3be22bd43ba3bd985756a3c4a05891b33f754433 Mon Sep 17 00:00:00 2001
From: Jeremy Ashkenas
- In JavaScript, it's extremely common to run into trouble when trying to - generate functions within a loop — all of the created functions share - the final value of loop variables. CoffeeScript provides a "scoped loop" - syntax for loops that should have function scope, similar to a forEach. - Note how the body of the loop gets lifted into a cached function. + When using a JavaScript loop to generate functions, it's common to insert + a closure wrapper in order to ensure that loop variables are closed over, + and all the generated functions don't just share the final values. CoffeeScript + provides the do keyword, which immediately invokes a passed function.
- <%= code_for('scoped_loops', 'funcs[3](2)') %> + <%= code_for('do') %>diff --git a/documentation/js/do.js b/documentation/js/do.js new file mode 100644 index 00000000..388f7f95 --- /dev/null +++ b/documentation/js/do.js @@ -0,0 +1,10 @@ +var fileName, _fn, _i, _len; +_fn = function(fileName) { + return fs.readFile(fileName, function(err, contents) { + return compile(fileName, contents.toString()); + }); +}; +for (_i = 0, _len = list.length; _i < _len; _i++) { + fileName = list[_i]; + _fn(fileName); +} \ No newline at end of file diff --git a/documentation/js/scoped_loops.js b/documentation/js/scoped_loops.js deleted file mode 100644 index f4355e3c..00000000 --- a/documentation/js/scoped_loops.js +++ /dev/null @@ -1,10 +0,0 @@ -var funcs, i, _fn; -funcs = []; -_fn = function(i) { - funcs.push(function(number) { - return number + i; - }); -}; -for (i = 0; i <= 3; i++) { - _fn(i); -} \ No newline at end of file diff --git a/documentation/js/splices.js b/documentation/js/splices.js index a6f916f1..f5795813 100644 --- a/documentation/js/splices.js +++ b/documentation/js/splices.js @@ -1,3 +1,3 @@ -var numbers; +var numbers, _ref; numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; -[].splice.apply(numbers, [3, 4].concat([-3, -4, -5, -6])); \ No newline at end of file +[].splice.apply(numbers, [3, 4].concat(_ref = [-3, -4, -5, -6])), _ref; \ No newline at end of file diff --git a/index.html b/index.html index b7ffe3b9..dedc4712 100644 --- a/index.html +++ b/index.html @@ -908,36 +908,26 @@ ages = (function() { for own key, value of object
- In JavaScript, it's extremely common to run into trouble when trying to - generate functions within a loop — all of the created functions share - the final value of loop variables. CoffeeScript provides a "scoped loop" - syntax for loops that should have function scope, similar to a forEach. - Note how the body of the loop gets lifted into a cached function. + When using a JavaScript loop to generate functions, it's common to insert + a closure wrapper in order to ensure that loop variables are closed over, + and all the generated functions don't just share the final values. CoffeeScript + provides the do keyword, which immediately invokes a passed function.
-funcs = [] - -for i in [0..3] -> - funcs.push (number) -> number + i -
var funcs, i, _fn; -funcs = []; -_fn = function(i) { - funcs.push(function(number) { - return number + i; ++for fileName in list + do (fileName) -> + fs.readFile fileName, (err, contents) -> + compile fileName, contents.toString() +var fileName, _fn, _i, _len; +_fn = function(fileName) { + return fs.readFile(fileName, function(err, contents) { + return compile(fileName, contents.toString()); }); }; -for (i = 0; i <= 3; i++) { - _fn(i); +for (_i = 0, _len = list.length; _i < _len; _i++) { + fileName = list[_i]; + _fn(fileName); } -
@@ -970,12 +960,12 @@ middle = copy.slice(3, 7);;alert(middle);'>run: middle
numbers[3..6] = [-3, -4, -5, -6]
-
var numbers;
+
var numbers, _ref; numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; -[].splice.apply(numbers, [3, 4].concat([-3, -4, -5, -6])); -
Note that JavaScript strings are immutable, and can't be spliced.
diff --git a/test/test_functions.coffee b/test/test_functions.coffee index 193839c6..4123f801 100644 --- a/test/test_functions.coffee +++ b/test/test_functions.coffee @@ -22,18 +22,18 @@ obj = { name: 'Fred' bound: -> - (=> eq this, obj)() + do (=> eq this, obj) unbound: -> - (-> ok this isnt obj)() + do (-> ok this isnt obj) nested: -> (=> - (=> + do (=> (=> eq this, obj )() - )() + ) )() }