2010-07-25 05:23:37 +00:00
|
|
|
area = (x, y, x1, y1) ->
|
2010-02-17 00:38:52 +00:00
|
|
|
(x - x1) * (x - y1)
|
|
|
|
|
2010-07-25 05:23:37 +00:00
|
|
|
x = y = 10
|
|
|
|
x1 = y1 = 20
|
2010-02-17 00:38:52 +00:00
|
|
|
|
2010-04-03 14:39:32 +00:00
|
|
|
ok area(x, y, x1, y1) is 100
|
2010-02-17 00:38:52 +00:00
|
|
|
|
2010-08-08 21:37:28 +00:00
|
|
|
# ok(area(x, y,
|
|
|
|
# x1, y1) is 100)
|
2010-02-17 00:38:52 +00:00
|
|
|
|
|
|
|
ok(area(
|
|
|
|
x
|
|
|
|
y
|
|
|
|
x1
|
|
|
|
y1
|
2010-04-03 14:39:32 +00:00
|
|
|
) is 100)
|
2010-02-17 00:38:52 +00:00
|
|
|
|
|
|
|
|
2010-07-25 05:23:37 +00:00
|
|
|
sumOfArgs = ->
|
|
|
|
sum = 0
|
2010-10-24 18:11:09 +00:00
|
|
|
sum += val for val in arguments
|
2010-03-16 03:46:14 +00:00
|
|
|
sum
|
|
|
|
|
2010-07-28 05:54:36 +00:00
|
|
|
ok sumOfArgs(1, 2, 3, 4, 5) is 15
|
|
|
|
|
|
|
|
|
|
|
|
((@arg) ->).call context = {}, 1
|
|
|
|
ok context.arg is 1
|
|
|
|
|
|
|
|
((splat..., @arg) ->).call context, 1, 2, 3
|
2010-10-26 11:51:02 +00:00
|
|
|
eq context.arg, 3
|
2010-07-28 06:17:50 +00:00
|
|
|
|
|
|
|
((@arg...) ->).call context, 1, 2, 3
|
2010-10-26 11:51:02 +00:00
|
|
|
eq context.arg.join(' '), '1 2 3'
|
2010-07-28 11:34:28 +00:00
|
|
|
|
|
|
|
class Klass
|
2010-11-14 19:21:55 +00:00
|
|
|
constructor: (@one, @two) ->
|
2010-07-28 11:34:28 +00:00
|
|
|
|
|
|
|
obj = new Klass 1, 2
|
|
|
|
|
2010-10-26 11:51:02 +00:00
|
|
|
eq obj.one, 1
|
|
|
|
eq obj.two, 2
|
2010-10-26 04:09:46 +00:00
|
|
|
|
|
|
|
|
2010-10-26 19:46:03 +00:00
|
|
|
# Destructuring.
|
|
|
|
(([{a: [b], c}]...) ->
|
|
|
|
eq b, 123
|
|
|
|
eq c, 456
|
|
|
|
) {a: [123], c: 456}
|
|
|
|
|
|
|
|
|
|
|
|
# Default values.
|
2010-10-26 04:09:46 +00:00
|
|
|
obj = f: (q = 123, @p = 456) -> q
|
|
|
|
eq obj.f(), 123
|
|
|
|
eq obj.p , 456
|
2010-10-26 11:51:02 +00:00
|
|
|
|
|
|
|
withSplats = (a = 2, b..., c = 3, d = 5) -> a * (b.length + 1) * c * d
|
|
|
|
eq 30, withSplats()
|
|
|
|
eq 15, withSplats 1
|
|
|
|
eq 5, withSplats 1, 1
|
|
|
|
eq 1, withSplats 1, 1, 1
|
|
|
|
eq 2, withSplats 1, 1, 1, 1
|