Questions tagged [coffeescript]

A language that transpiles to JavaScript. Use this tag for questions about programming with the CoffeeScript language.

From the CoffeeScript Homepage:

CoffeeScript is a little language that compiles into JavaScript. Underneath that awkward Java-esque patina, JavaScript has always had a gorgeous heart. CoffeeScript is an attempt to expose the good parts of JavaScript in a simple way.

CoffeeScript can be used in any JavaScript environment. Included in a web page, it supports <script type="text/coffeescript"> tags; with node.js it can compile code on the command line, and it is also available as a Ruby gem.

The documentation and downloads can be found on the CoffeeScript website.

Common causes of unexpected behavior

  • Check your whitespace. Indentation is SIGNIFICANT. Mixing SPACE and TAB in indentation will create problems. Make sure you understand the rules for whitespace and make sure you triple-check this.

Popular questions

###Freely available CoffeeScript programming books

Video Tutorial

Codeschool CoffeeScript Tutorial - Awesome place to start with CoffeeScript.

9747 questions
86
votes
1 answer

Static classes and methods in coffeescript

I want to write a static helper class in coffeescript. Is this possible? class: class Box2DUtility constructor: () -> drawWorld: (world, context) -> using: Box2DUtility.drawWorld(w,c);
Shawn Mclean
  • 56,733
  • 95
  • 279
  • 406
84
votes
11 answers

Private members in CoffeeScript?

Does somebody know how to make private, non-static members in CoffeeScript? Currently I'm doing this, which just uses a public variable starting with an underscore to clarify that it shouldn't be used outside of the class: class Thing extends…
thejh
  • 44,854
  • 16
  • 96
  • 107
81
votes
8 answers

Are there any languages that compile to Bash?

I both love and hate writing Bash. I love that it's so streamlined for operating on files and working with processes (I agree with this popular question that it's way better in this regard than Python, Ruby, etc.), but I hate the syntax,…
Aseem Kishore
  • 10,404
  • 10
  • 51
  • 56
80
votes
7 answers

Function declaration in CoffeeScript

I notice that in CoffeeScript, if I define a function using: a = (c) -> c=1 I can only get the function expression: var a; a = function(c) { return c = 1; }; But, personally I often use function declaration,for example: function a(c) { …
Grace Huang
  • 5,355
  • 5
  • 30
  • 52
78
votes
7 answers

Conditional operator in Coffeescript

I really like this: var value = maxValue > minValue ? minValue : maxValue; Is there something equally concise in Coffescript?
Blub
  • 13,014
  • 18
  • 75
  • 102
78
votes
2 answers

coffeescript check if not in array

Here's something simple to check if user is in moderator. But I want to check if user is not in moderator. if err && user in moderators return Intuitively it would be like this if err && user isnt in moderators return But obviously this…
Harry
  • 52,711
  • 71
  • 177
  • 261
76
votes
2 answers

for (var key in object) in CoffeeScript?

How can I use for (var key in object) in CoffeeScript? It compiles to... for (_i = 0, _len = object.length; _i < _len; _i++) { key = object[_i]; ...but I just want to iterate though an object.
fancy
  • 48,619
  • 62
  • 153
  • 231
75
votes
7 answers

Examples of CoffeeScript in NodeJS?

As a pet project, I am trying to get familiar with NodeJS and CoffeeScript, and am finding it hard to get the ball rolling. I've found plenty of examples of single-file super-simple apps like in CoffeeScript's examples folder, however, none of those…
Austin Hyde
  • 26,347
  • 28
  • 96
  • 129
74
votes
5 answers

Create an ISO date object in javascript

I have a mongo database set up. creating a new date object in mongoDb create a date object in ISO format eg: ISODate("2012-07-14T00:00:00Z") I am using node.js to connect to mongo database and query the database. when ever I create a new date…
Joel James
  • 3,870
  • 9
  • 32
  • 47
73
votes
3 answers

How can I convert a JavaScript for-loop to CoffeeScript?

for (i = 0; i < 10; i++) { doStuff(); } That's the JavaScript code that I Want to convert to CoffeeScript.
Shamoon
  • 41,293
  • 91
  • 306
  • 570
68
votes
5 answers

How to use mongoose findOne

I have the below schema (apologies that it is in coffeescript) Schema = mongoose.Schema AuthS = new Schema auth: {type: String, unique: true} nick: String time: Date Auth = mongoose.model 'Auth', AuthS I simply want to recover…
Callum Rogers
  • 15,630
  • 17
  • 67
  • 90
68
votes
5 answers

How do I properly format long compound if statements in Coffeescript

If I had a complex if statement that I did not want to overflow simply for aesthetic purposes, what would be the most kosher way to break it up since coffeescript will interpret returns as the body of the statement in this case? if (foo is…
Evan
  • 7,396
  • 4
  • 32
  • 31
68
votes
5 answers

Can I write npm package in CoffeeScript?

I have used CoffeeScript for a while. Now I need to write a npm package, can I write it in CoffeeScript, or I should compile CoffeeScript into JavaScript?
Zhe Chen
  • 2,918
  • 4
  • 23
  • 39
65
votes
4 answers

Method chaining with function arguments

What's the best way to chain methods in CoffeeScript? For example, if I have this JavaScript how could I write it in CoffeeScript? var req = $.get('foo.htm') .success(function( response ){ // do something // ... }) .error(function(){ …
nicholaides
  • 19,211
  • 12
  • 66
  • 82
63
votes
5 answers

A Backbone.js Collection of multiple Model subclasses

I have a REST Json API that returns a list "logbooks". There are many types of logbooks that implement different but similar behavior. The server side implementation of this on the Database layer is a sort of Single Table Inheritance, so each JSON…
Tricote
  • 1,508
  • 1
  • 13
  • 21