2010-06-12 19:05:13 -04:00
|
|
|
ok(if mySpecialVariable? then false else true)
|
2010-01-01 12:41:55 -05:00
|
|
|
|
2010-07-25 01:23:37 -04:00
|
|
|
mySpecialVariable = false
|
2010-01-01 12:41:55 -05:00
|
|
|
|
2010-06-12 19:05:13 -04:00
|
|
|
ok(if mySpecialVariable? then true else false)
|
2010-01-16 23:03:54 -05:00
|
|
|
|
|
|
|
|
|
|
|
# Existential assignment.
|
2010-07-25 01:23:37 -04:00
|
|
|
a = 5
|
|
|
|
a = null
|
2010-01-16 23:03:54 -05:00
|
|
|
a ?= 10
|
|
|
|
b ?= 10
|
|
|
|
|
2010-02-16 19:45:25 -05:00
|
|
|
ok a is 10 and b is 10
|
2010-01-16 23:03:54 -05:00
|
|
|
|
|
|
|
|
|
|
|
# The existential operator.
|
2010-07-25 01:23:37 -04:00
|
|
|
z = null
|
|
|
|
x = z ? "EX"
|
2010-02-16 19:45:25 -05:00
|
|
|
ok z is null and x is "EX"
|
2010-01-17 14:23:41 -05:00
|
|
|
|
2010-08-14 16:24:05 -04:00
|
|
|
i = 9
|
|
|
|
func = -> i += 1
|
|
|
|
result = func() ? 101
|
|
|
|
ok result is 10
|
|
|
|
|
2010-08-14 16:33:20 -04:00
|
|
|
ok (non ? existent ? variables ? 1) is 1
|
|
|
|
|
2010-01-17 14:23:41 -05:00
|
|
|
|
|
|
|
# Only evaluate once.
|
2010-07-25 01:23:37 -04:00
|
|
|
counter = 0
|
|
|
|
getNextNode = ->
|
2010-01-17 14:23:41 -05:00
|
|
|
throw "up" if counter
|
|
|
|
counter++
|
|
|
|
|
2010-06-12 19:05:13 -04:00
|
|
|
ok(if getNextNode()? then true else false)
|
2010-01-23 23:30:55 -05:00
|
|
|
|
|
|
|
|
|
|
|
# Existence chains, soaking up undefined properties:
|
2010-07-25 01:23:37 -04:00
|
|
|
obj = {
|
2010-01-23 23:30:55 -05:00
|
|
|
prop: "hello"
|
|
|
|
}
|
|
|
|
|
2010-02-16 19:45:25 -05:00
|
|
|
ok obj?.prop is "hello"
|
2010-01-23 23:30:55 -05:00
|
|
|
|
2010-02-24 00:06:01 -05:00
|
|
|
ok obj?['prop'] is "hello"
|
|
|
|
|
2010-02-16 19:45:25 -05:00
|
|
|
ok obj.prop?.length is 5
|
2010-02-02 10:39:44 -05:00
|
|
|
|
2010-02-24 00:06:01 -05:00
|
|
|
ok obj?['prop']?['length'] is 5
|
|
|
|
|
2010-02-16 19:45:25 -05:00
|
|
|
ok obj?.prop?.non?.existent?.property is undefined
|
2010-01-24 12:52:15 -05:00
|
|
|
|
2010-02-24 00:06:01 -05:00
|
|
|
ok obj?['non']?['existent'].property is undefined
|
|
|
|
|
2010-01-24 12:52:15 -05:00
|
|
|
|
|
|
|
# Soaks and caches method calls as well.
|
2010-07-25 01:23:37 -04:00
|
|
|
arr = ["--", "----"]
|
2010-01-24 12:52:15 -05:00
|
|
|
|
2010-02-16 19:45:25 -05:00
|
|
|
ok arr.pop()?.length is 4
|
|
|
|
ok arr.pop()?.length is 2
|
|
|
|
ok arr.pop()?.length is undefined
|
|
|
|
ok arr[0]?.length is undefined
|
|
|
|
ok arr.pop()?.length?.non?.existent()?.property is undefined
|
2010-02-20 20:00:07 -05:00
|
|
|
|
|
|
|
|
|
|
|
# Soaks method calls safely.
|
2010-07-25 01:23:37 -04:00
|
|
|
value = undefined
|
|
|
|
result = value?.toString().toLowerCase()
|
2010-02-20 20:00:07 -05:00
|
|
|
|
|
|
|
ok result is undefined
|
|
|
|
|
2010-07-25 01:23:37 -04:00
|
|
|
value = 10
|
|
|
|
result = value?.toString().toLowerCase()
|
2010-02-20 20:00:07 -05:00
|
|
|
|
|
|
|
ok result is '10'
|
|
|
|
|
2010-02-22 22:11:47 -05:00
|
|
|
|
2010-08-14 11:29:10 -04:00
|
|
|
# Soaks inner values.
|
|
|
|
ident = (obj) -> obj
|
|
|
|
ok ident(non?.existent().method()) is undefined
|
|
|
|
|
|
|
|
|
2010-07-15 09:08:51 -04:00
|
|
|
# Soaks constructor invocations.
|
2010-07-25 01:23:37 -04:00
|
|
|
a = 0
|
2010-07-15 09:08:51 -04:00
|
|
|
class Foo
|
|
|
|
constructor: -> a += 1
|
|
|
|
bar: "bat"
|
|
|
|
|
|
|
|
ok (new Foo())?.bar is 'bat'
|
|
|
|
ok a is 1
|
|
|
|
|
|
|
|
|
2010-02-22 22:11:47 -05:00
|
|
|
# Safely existence test on soaks.
|
2010-07-25 01:23:37 -04:00
|
|
|
result = not value?.property?
|
2010-02-22 22:11:47 -05:00
|
|
|
ok result
|
|
|
|
|
2010-05-05 05:58:48 -04:00
|
|
|
|
|
|
|
# Safely calls values off of non-existent variables.
|
2010-07-25 01:23:37 -04:00
|
|
|
result = nothing?.value
|
2010-05-05 05:58:48 -04:00
|
|
|
ok result is undefined
|
2010-07-10 11:27:43 -04:00
|
|
|
|
|
|
|
|
|
|
|
# Assign to the result of an exsitential operation with a minus.
|
2010-07-25 01:23:37 -04:00
|
|
|
x = null ? - 1
|
2010-07-10 11:27:43 -04:00
|
|
|
ok x is - 1
|
2010-07-15 21:38:35 -04:00
|
|
|
|
|
|
|
|
|
|
|
# Things that compile to ternaries should force parentheses, like operators do.
|
2010-07-25 01:23:37 -04:00
|
|
|
duration = if options?.animated then 150 else 0
|
2010-07-15 21:38:35 -04:00
|
|
|
ok duration is 0
|