jashkenas--coffeescript/test/fixtures/execution/test_functions.coffee

55 lines
925 B
CoffeeScript
Raw Normal View History

2010-01-14 01:59:57 +00:00
x: 1
y: {}
y.x: => 3
2010-01-14 01:59:57 +00:00
print x is 1
print typeof(y.x) is 'function'
print y.x() is 3
print y.x.name is 'x'
2010-01-14 01:59:57 +00:00
# The empty function should not cause a syntax error.
=>
2010-01-14 01:59:57 +00:00
obj: {
name: "Fred"
bound: =>
(==> print(this.name is "Fred"))()
2010-01-14 01:59:57 +00:00
unbound: =>
(=> print(!this.name?))()
2010-01-14 01:59:57 +00:00
}
obj.unbound()
obj.bound()
# The named function should be cleared out before a call occurs:
# Python decorator style wrapper that memoizes any function
memoize: (fn) =>
cache: {}
self: this
(args...) =>
key: args.toString()
return cache[key] if cache[key]
cache[key] = fn.apply(self, args)
Math: {
Add: (a, b) => a + b
AnonymousAdd: ((a, b) => a + b)
FastAdd: memoize (a, b) => a + b
}
print Math.Add(5, 5) is 10
print Math.AnonymousAdd(10, 10) is 20
print Math.FastAdd(20, 20) is 40
# Parens are optional on simple function calls.
print 100 > 1 if 1 > 0
print true unless false
print true for i in [1..3]