2010-01-14 01:59:57 +00:00
|
|
|
x: 1
|
|
|
|
y: {}
|
|
|
|
y.x: => 3
|
|
|
|
|
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-14 01:59:57 +00:00
|
|
|
obj: {
|
|
|
|
name: "Fred"
|
|
|
|
|
|
|
|
bound: =>
|
|
|
|
(==> print(this.name is "Fred"))()
|
|
|
|
|
|
|
|
unbound: =>
|
|
|
|
(=> print(!this.name?))()
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
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)
|
2010-01-25 04:40:45 +00:00
|
|
|
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
|