Navigation Menu

Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

F A Q #2316

Closed
vendethiel opened this issue May 11, 2012 · 3 comments
Closed

F A Q #2316

vendethiel opened this issue May 11, 2012 · 3 comments

Comments

@vendethiel
Copy link
Collaborator

Hello guys.

Just went on the irc channel to ask something, which I think has been answered numerous time.
Jashkenas offered to set up a FAQ if there's good questions in it, so I'd like to ask you to come with the questions you had, that now looks obvious to you after it has been explained, and which surely has been explained a loot of time.
I'd like to start with :

Why does CoffeeScript requires "foo" to be defined when doing foo ?= value or foo ||= value

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 at the current scope, you know it doesn't exist - Javascript has no mecanic like php's static keyword.
Note that it perfectly works 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 a "static"-like access to "staticVariable"
Why is CoffeeScript sometimes using ["bar"] notation over .bar ?

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, ...) reserved words were not allowed as the right operand in a member access. See this comment or more information

Why is the existantial "?" operator only checking this.foo != null, shouldn't it also check for typeof === 'undefined' ?

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 ReferenceErrors. See the Abstract Equality Comparaison Algorithm (section 11.9.3) for more informations (especially steps 2 and 3)

(see #1869, #1631)

(I'd like to thank @jashkenas for the first one, and @michaelficarra for his infinite patience for the two others).

@michaelficarra
Copy link
Collaborator

This is a great start. Thanks @Nami-Doc.

edit: You could mention a CS equivalent to PHP's static keyword:

do (staticVar = initialValue) ->
  (args...) -> # this function now has "static"-like access to `staticVar`

Put that anywhere you would put the inner function. We have created a [closure](http://en.wikipedia.org/wiki/Closure_(computer_science\)).

edit again: Your "this comment" link has an f on the end, which breaks it. You should probably inline the comment to make it easier.

@vendethiel
Copy link
Collaborator Author

Thanks @michaelficarra ! I edited my post.

@vendethiel
Copy link
Collaborator Author

closing since it's now a wiki page (I probably need to link it somewhere, tho)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants