2009-12-13 17:23:10 -05:00
|
|
|
# Functions:
|
2009-12-16 21:01:47 -05:00
|
|
|
square: x => x * x.
|
|
|
|
|
|
|
|
sum: x, y => x + y.
|
|
|
|
|
|
|
|
odd: x => x % 2 is 0.
|
|
|
|
|
2009-12-24 20:21:20 -05:00
|
|
|
even: x => x % 2 isnt 0.
|
2009-12-16 21:01:47 -05:00
|
|
|
|
|
|
|
run_loop: =>
|
|
|
|
fire_events( e => e.stopPropagation(). )
|
|
|
|
listen()
|
|
|
|
wait().
|
|
|
|
|
|
|
|
# Objects:
|
|
|
|
dense_object_literal: {one: 1, two: 2, three: 3}
|
2009-12-16 20:48:37 -05:00
|
|
|
|
|
|
|
spaced_out_multiline_object: {
|
2009-12-13 18:37:29 -05:00
|
|
|
pi: 3.14159
|
2009-12-17 21:00:31 -05:00
|
|
|
list: [1, 2, 3, 4]
|
2009-12-17 08:29:19 -05:00
|
|
|
regex: /match[ing](every|thing|\/)/gi
|
2009-12-14 10:00:31 -05:00
|
|
|
three: new Idea()
|
2009-12-16 20:48:37 -05:00
|
|
|
|
2009-12-13 18:37:29 -05:00
|
|
|
inner_obj: {
|
|
|
|
freedom: => _.freedom().
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
# Arrays:
|
|
|
|
stooges : [{moe: 45}, {curly: 43}, {larry: 46}]
|
|
|
|
|
|
|
|
exponents : [x => x., x => x * x., x => x * x * x.]
|
|
|
|
|
2009-12-16 21:01:47 -05:00
|
|
|
empty: []
|
|
|
|
|
2009-12-17 22:13:29 -05:00
|
|
|
multiline: [
|
|
|
|
'line one'
|
|
|
|
'line two'
|
|
|
|
]
|
|
|
|
|
2009-12-13 18:37:29 -05:00
|
|
|
# Conditionals and ternaries.
|
|
|
|
if submarine.shields_up
|
|
|
|
full_speed_ahead()
|
2009-12-15 20:49:24 -05:00
|
|
|
fire_torpedos()
|
2009-12-18 00:49:23 -05:00
|
|
|
else if submarine.sinking
|
|
|
|
abandon_ship()
|
2009-12-13 18:37:29 -05:00
|
|
|
else
|
|
|
|
run_away().
|
|
|
|
|
|
|
|
eldest: if 25 > 21 then liz else marge.
|
|
|
|
|
2009-12-15 08:53:21 -05:00
|
|
|
decoration: medal_of_honor if war_hero
|
|
|
|
|
|
|
|
go_to_sleep() unless coffee
|
|
|
|
|
2009-12-13 18:37:29 -05:00
|
|
|
# Returning early:
|
|
|
|
race: =>
|
|
|
|
run()
|
|
|
|
walk()
|
|
|
|
crawl()
|
|
|
|
if tired then return sleep().
|
|
|
|
race().
|
2009-12-13 17:07:16 -05:00
|
|
|
|
2009-12-17 21:46:12 -05:00
|
|
|
# Conditional assignment:
|
2009-12-25 10:31:51 -05:00
|
|
|
good ||= evil
|
|
|
|
wine &&= cheese
|
2009-12-13 20:29:44 -05:00
|
|
|
|
2009-12-14 00:06:08 -05:00
|
|
|
# Nested property access and calls.
|
2009-12-17 22:13:29 -05:00
|
|
|
((moon.turn(360))).shapes[3].move({x: 45, y: 30}).position['top'].offset('x')
|
2009-12-14 00:06:08 -05:00
|
|
|
|
2009-12-15 10:35:17 -05:00
|
|
|
a: b: c: 5
|
|
|
|
|
2009-12-15 09:11:27 -05:00
|
|
|
# Embedded JavaScript.
|
|
|
|
callback(
|
|
|
|
`function(e) { e.stop(); }`
|
|
|
|
)
|
|
|
|
|
2009-12-14 23:03:51 -05:00
|
|
|
# Try/Catch/Finally/Throw.
|
2009-12-14 10:00:31 -05:00
|
|
|
try
|
|
|
|
all_hell_breaks_loose()
|
|
|
|
dogs_and_cats_living_together()
|
2009-12-14 23:03:51 -05:00
|
|
|
throw "up"
|
2009-12-14 10:00:31 -05:00
|
|
|
catch error
|
2009-12-14 23:03:51 -05:00
|
|
|
print( error )
|
|
|
|
finally
|
|
|
|
clean_up().
|
2009-12-13 17:07:16 -05:00
|
|
|
|
2009-12-15 00:27:34 -05:00
|
|
|
try all_hell_breaks_loose() catch error print(error) finally clean_up().
|
|
|
|
|
2009-12-17 21:10:49 -05:00
|
|
|
# While loops, break and continue.
|
2009-12-15 00:27:34 -05:00
|
|
|
while demand > supply
|
|
|
|
sell()
|
|
|
|
restock().
|
|
|
|
|
|
|
|
while supply > demand then buy().
|
|
|
|
|
2009-12-17 21:10:49 -05:00
|
|
|
while true
|
|
|
|
break if broken
|
|
|
|
continue if continuing.
|
|
|
|
|
2009-12-15 00:27:34 -05:00
|
|
|
# Unary operators.
|
2009-12-24 00:37:33 -05:00
|
|
|
!!true
|
2009-12-15 00:27:34 -05:00
|
|
|
|
2009-12-15 10:07:10 -05:00
|
|
|
# Lexical scoping.
|
|
|
|
a: 5
|
|
|
|
change_a_and_set_b: =>
|
|
|
|
a: 10
|
|
|
|
b: 15.
|
|
|
|
b: 20
|
|
|
|
|
2009-12-15 10:28:54 -05:00
|
|
|
# Array comprehensions.
|
2009-12-18 23:13:59 -05:00
|
|
|
supper: food.capitalize() for food in ['toast', 'cheese', 'wine'].
|
2009-12-15 10:28:54 -05:00
|
|
|
|
2009-12-18 23:13:59 -05:00
|
|
|
drink(bottle) for bottle, i in ['soda', 'wine', 'lemonade'] if even(i).
|
2009-12-15 22:30:27 -05:00
|
|
|
|
2009-12-17 21:14:36 -05:00
|
|
|
# Switch statements ("else" serves as a default).
|
2009-12-22 10:16:53 -05:00
|
|
|
activity: switch day
|
2009-12-24 04:33:59 -05:00
|
|
|
when "Tuesday" then eat_breakfast()
|
|
|
|
when "Sunday" then go_to_church()
|
|
|
|
when "Saturday" then go_to_the_park()
|
|
|
|
when "Wednesday"
|
2009-12-22 10:16:53 -05:00
|
|
|
if day is bingo_day
|
|
|
|
go_to_bingo()
|
|
|
|
else
|
|
|
|
eat_breakfast()
|
|
|
|
go_to_work()
|
|
|
|
eat_dinner().
|
2009-12-17 21:14:36 -05:00
|
|
|
else go_to_work().
|
2009-12-16 20:19:52 -05:00
|
|
|
|
|
|
|
# Semicolons can optionally be used instead of newlines.
|
2009-12-16 21:43:26 -05:00
|
|
|
wednesday: => eat_breakfast(); go_to_work(); eat_dinner(); .
|
|
|
|
|
|
|
|
# Array slice literals.
|
|
|
|
zero_to_nine: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
|
2009-12-25 19:20:28 -05:00
|
|
|
three_to_six: zero_to_nine[3..6]
|
2009-12-17 08:23:07 -05:00
|
|
|
|
2009-12-17 08:26:46 -05:00
|
|
|
# Multiline strings with inner quotes.
|
|
|
|
story: "Lorem ipsum dolor \"sit\" amet, consectetuer adipiscing elit,
|
2009-12-17 08:23:07 -05:00
|
|
|
sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna
|
2009-12-17 09:07:42 -05:00
|
|
|
aliquam erat volutpat. Ut wisi enim ad."
|
|
|
|
|
2009-12-21 11:41:45 -05:00
|
|
|
# Inheritance and calling super.
|
|
|
|
Animal: => .
|
|
|
|
Animal.prototype.move: meters =>
|
|
|
|
alert(this.name + " moved " + meters + "m.").
|
|
|
|
|
|
|
|
Snake: name => this.name: name.
|
2009-12-25 16:57:47 -05:00
|
|
|
Snake extends Animal
|
2009-12-21 11:41:45 -05:00
|
|
|
Snake.prototype.move: =>
|
|
|
|
alert('Slithering...')
|
|
|
|
super(5).
|
|
|
|
|
|
|
|
Horse: name => this.name: name.
|
2009-12-25 16:57:47 -05:00
|
|
|
Horse extends Animal
|
2009-12-21 11:41:45 -05:00
|
|
|
Horse.prototype.move: =>
|
|
|
|
alert('Galloping...')
|
|
|
|
super(45).
|
|
|
|
|
|
|
|
sam: new Snake("Sammy the Snake")
|
|
|
|
tom: new Horse("Tommy the Horse")
|
|
|
|
|
|
|
|
sam.move()
|
|
|
|
tom.move()
|
2009-12-17 21:00:31 -05:00
|
|
|
|
|
|
|
# Numbers.
|
2009-12-17 21:46:12 -05:00
|
|
|
a_googol: 1e100
|
|
|
|
hex: 0xff0000
|
|
|
|
negative: -1.0
|
|
|
|
infinity: Infinity
|
|
|
|
nan: NaN
|
2009-12-17 21:21:07 -05:00
|
|
|
|
|
|
|
# Deleting.
|
|
|
|
delete secret.identity
|