jashkenas--coffeescript/test/test_arguments.coffee

64 lines
938 B
CoffeeScript
Raw Normal View History

2010-07-25 05:23:37 +00:00
area = (x, y, x1, y1) ->
(x - x1) * (x - y1)
2010-07-25 05:23:37 +00:00
x = y = 10
x1 = y1 = 20
ok area(x, y, x1, y1) is 100
# ok(area(x, y,
# x1, y1) is 100)
ok(area(
x
y
x1
y1
) is 100)
2010-07-25 05:23:37 +00:00
sumOfArgs = ->
sum = 0
sum += val for val in arguments
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
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
# 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