I have a little problem with CoffeeScript. I'm trying to build a RESTful API in Node.js and Express. Here is the problematic part of the code:
express = require('express');
router = express.Router()
router.route '/todos/:todo_id'
.get (req, res) ->
# do get ...
.delete (req, res) ->
# do delete ...
.put (req, res) ->
# do put ...
Which compiles to:
router.route('/todos/:todo_id')
.get(function(req, res) {
# here's my get code
})["delete"](function(req, res) {
# here's my delete code
}).put(function(req, res) {
# here's my put code
});
The "delete" keyword seems to be tricky - when I replace it with any other word (get, put, delet, whatever), it compiles to .keyword just fine, but only "delete" keyword compiles to array-access-thing. I can't use any other keyword because of REST. Any ideas? Thank you.
Because delete is a keyword, as you noticed yourself, you can't use it as the name of an identifier. But, since object properties in JavaScript can be accessed using [] as well, you can still define (and use) a method with the name of a keyword by using the bracket notation.
CoffeeScript knows that, and automatically compiles it correctly for you. This should have no impact on functionality.