-
Notifications
You must be signed in to change notification settings - Fork 130
Common Gotchas
This page lists common gotchas you may encounter when working with CoffeeScript. You can refer to this page for general answers on "why does CoffeeScript act that way"
Q: Why does CoffeeScript require "foo" to be defined when doing foo ?= value
or foo ||= value
A: Otherwise, it'd create a global, which is not what we want (if that is what you want, use window.foo ?= value
)
If you're declaring the variable in the current scope, you know for sure it doesn't exist - Javascript has no mechanic like PHP's static keyword.
Note that it works perfectly when used with classes :
class Foo
getCache: ->
@cache ?= "value"
If you want an alternative to PHP's static
keyword, you can use a closure :
do (staticVariable = theValueYouWant) ->
(args...) ->
#you now have "static"-like access to "staticVariable"
Q: Why is CoffeeScript sometimes using ["bar"]
notation over .bar
?
A: CoffeeScript detects reserved keywords (as the auto-quoting of keywords in array notation) and prefer to use the array-access syntax ([]
), because in ES3, reserved keywords (throw
, class
, ...) were not allowed as the right operand in a member access. See this comment for more information.
Q: Why is the existential "?" operator only checking this.foo != null
, shouldn't it also check for typeof === 'undefined'
?
A: X == null
tests that either X is null or undefined, assuming it is in scope. If we can't make that assumption, we need to do a typeof
test to avoid ReferenceError
s. See the Abstract Equality Comparaison Algorithm (section 11.9.3) for more information (especially steps 2 and 3).
Q: Why is foo +a
different from foo + a
?
A: CoffeeScript is a whitespace-significant language. foo<space>
starts an implicit call, you must either dually-space your operator or dually-unspace it.