2010-01-14 01:59:57 +00:00
|
|
|
x: 1
|
|
|
|
y: {}
|
2010-01-26 07:15:08 +00:00
|
|
|
y.x: => 3
|
2010-01-14 01:59:57 +00:00
|
|
|
|
2010-01-25 04:40:45 +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
|
|
|
|
|
|
|
|
2010-01-16 20:02:04 +00:00
|
|
|
# The empty function should not cause a syntax error.
|
2010-01-26 07:15:08 +00:00
|
|
|
=>
|
2010-01-16 20:02:04 +00:00
|
|
|
|
|
|
|
|
2010-01-14 01:59:57 +00:00
|
|
|
obj: {
|
|
|
|
name: "Fred"
|
|
|
|
|
2010-01-26 07:15:08 +00:00
|
|
|
bound: =>
|
|
|
|
(==> print(this.name is "Fred"))()
|
2010-01-14 01:59:57 +00:00
|
|
|
|
2010-01-26 07:15:08 +00:00
|
|
|
unbound: =>
|
|
|
|
(=> print(!this.name?))()
|
2010-01-14 01:59:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
obj.unbound()
|
2010-01-16 16:24:10 +00:00
|
|
|
obj.bound()
|
2010-01-16 20:44:07 +00:00
|
|
|
|
|
|
|
|
|
|
|
# The named function should be cleared out before a call occurs:
|
|
|
|
|
|
|
|
# Python decorator style wrapper that memoizes any function
|
2010-01-26 05:40:58 +00:00
|
|
|
memoize: (fn) =>
|
2010-01-16 20:44:07 +00:00
|
|
|
cache: {}
|
|
|
|
self: this
|
2010-01-26 05:40:58 +00:00
|
|
|
(args...) =>
|
2010-01-16 20:44:07 +00:00
|
|
|
key: args.toString()
|
|
|
|
return cache[key] if cache[key]
|
|
|
|
cache[key] = fn.apply(self, args)
|
|
|
|
|
|
|
|
Math: {
|
2010-01-26 05:40:58 +00:00
|
|
|
Add: (a, b) => a + b
|
|
|
|
AnonymousAdd: ((a, b) => a + b)
|
|
|
|
FastAdd: memoize (a, b) => a + b
|
2010-01-16 20:44:07 +00:00
|
|
|
}
|
|
|
|
|
2010-01-25 04:40:45 +00:00
|
|
|
print Math.Add(5, 5) is 10
|
|
|
|
print Math.AnonymousAdd(10, 10) is 20
|
|
|
|
print Math.FastAdd(20, 20) is 40
|
2010-01-25 04:56:27 +00:00
|
|
|
|
|
|
|
|
|
|
|
# Parens are optional on simple function calls.
|
|
|
|
print 100 > 1 if 1 > 0
|
|
|
|
print true unless false
|
|
|
|
print true for i in [1..3]
|
2010-01-26 07:27:19 +00:00
|
|
|
|
|
|
|
print_func: (f) => print(f())
|
|
|
|
print_func => true
|