Last active
September 27, 2015 23:47
-
-
Save joannecheng/1350697 to your computer and use it in GitHub Desktop.
why I use coffeescript.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# For one, it's just nicer to look at. | |
# No more excessive brackets and semi colons | |
## js | |
function foo(bar){ | |
if (bar == 0){ | |
return "zero!"; | |
} else { | |
return "Not zero!"; | |
} | |
} | |
## coffee | |
foo = (bar) -> | |
if bar == 0 then "zero!" else "Not zero!" | |
# Notice the implicit returns! | |
# Declaring objects is much less painful. | |
# Missed commas/brackets are no fun. | |
## js | |
cities= { | |
NYC: { | |
population: "8 million", | |
location: "40.77, 73.98" | |
}, | |
Denver: { | |
population: "7 million. maybe", | |
location: "39.75 104.87" | |
} | |
} | |
## coffee | |
cities = | |
NYC: | |
population: "8 million" | |
location: "40.77, 73.98" | |
Denver: | |
population: "7 million. maybe" | |
location: "39.75 104.87" | |
# I also like the fact that coffee takes care of scope for you. | |
# No more missing var statements and unintentional global declarations (if you forget var); | |
## js | |
var five = "5"; | |
function setValue(){ | |
five = "6"; | |
return five; | |
} | |
console.log("The value of 'five' before setValue(): " + five ); #five = "5" | |
setValue(); #returns "6" | |
console.log("The value of 'five' after setValue(): " + five); #five = "6" | |
## coffee | |
five = "5" | |
setValue = () -> five = "6" | |
console.log "The value of 'five' before setValue(): #{five}" #five = "5" | |
setValue() #returns "6" | |
console.log "The value of 'five' after setValue(): #{five}" #five = "5" | |
# notice how it's easier to deal with strings in coffeescript! Multiline | |
# strings are allowed too. | |
## js | |
str = "Look! \nMultiline strings have to be typed out like this. \nKind of annoying, no?"; | |
## coffee | |
str = ''' | |
Look! | |
A multiline string! | |
THIS IS TOTALLY VALID. | |
''' | |
# Also, rather than using javascript's really awkward prototyping, you can use | |
# classes in coffeescript. | |
# I'm really tired, so I'm just going to link to | |
# http://jashkenas.github.com/coffee-script/ | |
### | |
This might have been excessive for a twitter question that could be | |
covered by a simple link to this page (once again): | |
http://jashkenas.github.com/coffee-script/ | |
but I wanted an excuse to explain something myself. I'm not very good at that. | |
After working in javascript for two years in an awful IDE, it's awesome to use | |
something like coffeescript. I found it pretty easy to pick up once I was able | |
to translate basic javascript concepts into coffeescript. It also works really well | |
with jquery, because I don't have to search through };);)}s all day. | |
Anyway, I hope you found it the least bit helpful. Best of luck! | |
### | |
######################################## | |
# Some minor annoyances... | |
######################################## | |
# The rules for parentheses are a little inconsistent. | |
# This is a valid way to call a function: | |
myFunction() | |
# This is also a valid way to call a function: | |
myFunction "Param" | |
# This is not how you call a function: | |
myFunction | |
# Objects vs Arrays... | |
obj = | |
name: "Joanne" | |
gender: "female" | |
height: "5' 3" | |
location: "Boulder, CO" | |
arr = ["Joanne", "female", "5' 3", "Boulder, CO"] | |
# works | |
for a in arr | |
console.log a | |
# works | |
for k, v of obj | |
console.log "Property: #{k}, Value: #{v}" | |
# NOPE. Even thought this would output the object key in regular js. | |
for o in obj | |
console.log o | |
# To call another function in a class/prototype, one uses | |
# "this", or just "@". So... | |
class LoadStuff: | |
constructor: () -> | |
@load_data() | |
load_data: () -> | |
... | |
@get_stuff(params) | |
get_stuff: (params) -> | |
.... | |
# but if I throw something with a callback in load_data, | |
# like a jquery statement, I can't use @function_name anymore. | |
# The context of "@"/"this" is changed. | |
.... | |
load_data: () -> | |
$.get(url, (data) -> | |
LoadStuff.prototype.get_stuff(data) | |
) | |
# But the wonderful fat arrow (=>) solves that issue. | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
No problem! I enjoy talking about javascript more than normal human beings. :X
Best of luck!