mirror of
https://github.com/jashkenas/coffeescript.git
synced 2026-04-11 03:00:13 -04:00
refactored loop variable caching
This commit is contained in:
@@ -45,55 +45,6 @@ ok 2 of evens
|
||||
# all/from/to aren't reserved.
|
||||
all = from = to = 1
|
||||
|
||||
# Ensure that the closure wrapper preserves local variables.
|
||||
obj = {}
|
||||
|
||||
for method in ['one', 'two', 'three']
|
||||
obj[method] = ->
|
||||
"I'm " + method
|
||||
|
||||
ok obj.one() is "I'm one"
|
||||
ok obj.two() is "I'm two"
|
||||
ok obj.three() is "I'm three"
|
||||
|
||||
i = 0
|
||||
for i in [1..3]
|
||||
-> 'func'
|
||||
break if false
|
||||
ok i is 3
|
||||
|
||||
|
||||
# Ensure that local variables are closed over for range comprehensions.
|
||||
funcs = for i from 1 to 3
|
||||
-> -i
|
||||
|
||||
ok (func() for func in funcs).join(' ') is '-1 -2 -3'
|
||||
ok i is 3
|
||||
|
||||
|
||||
# Ensure that closing over local variables doesn't break scoping laws.
|
||||
for i in [0]
|
||||
count = 0
|
||||
i = 50
|
||||
->
|
||||
ok count is 0
|
||||
ok i is 50
|
||||
|
||||
for [a, b] in [[0, 1]] then ->
|
||||
ok a is 0
|
||||
ok b is 1
|
||||
|
||||
|
||||
# Even when referenced in the filter.
|
||||
list = ['one', 'two', 'three']
|
||||
|
||||
methods = for num, i in list when num isnt 'two' and i isnt 1
|
||||
-> num + ' ' + i
|
||||
|
||||
ok methods.length is 2
|
||||
ok methods[0]() is 'one 0'
|
||||
ok methods[1]() is 'three 2'
|
||||
|
||||
|
||||
# Nested comprehensions.
|
||||
multiLiner =
|
||||
|
||||
Reference in New Issue
Block a user