Anonymous function can be written as just a dash plus an arrow:
1
->
This will be compiled to JavaScript:
1
(function(){});
This anonymous function can be invoked immediately:
1
2
// (->)()
(function(){})();
The immediately invoked function expression (IIFE) syntax is different from the preferred format in JavaScript. But this is more likely due to the syntax and compilation of CoffeeScript.
Here is one useful example to show the syntax:
1
2
3
4
5
6
// (square = (n) -> return n * n)()
var square;
(square = function(n){
return n * n;
})();
And a few more without compiled Javascript:
1
2
3
(->)()
(square = (n) -> return n * n)()
console.log(square = (n) -> return n * n)(10) # 100
Because CoffeeScript binary (coffee) use the following format:
#!/usr/bin/env node
Which does not specify fully qualified path on the shebang line, and will result in:
/usr/bin/env: node: No such fileordirectory
We can compile CoffeeScript into JavaScript and use fully qualified node executable. But this is not necessary. Since we are using Debian based system, which uses Vixie cron. It allows environment variables to be defined. We can make Cron aware of the custom path in environment variable:
If you are writing in CoffeeScript, then requiring another module written in CoffeeScript works the same as as both scripts are in JavaScript:
1
cm = require'coffee-module'
But if you are writing in JavaScript, and the dependent module is in CoffeeScript, you have to include CoffeeScript as a dependency with require statement:
1
2
require('coffee-script');
var cm = require('coffee-module');
For better compatibility, source codes written in CoffeeScript should be compiled into JavaScript. But what is the best practice? You don’t want to maintain two languages. When to compile? Here is two options:
Compile before publish the module
Compile after module install
The advantage of the first approach is that there is no dependency on CoffeeScript. The module has been compiled into JavaScript prior to submitting to module registry. However, this approach requires two repositories, one is source code repository, and another one is the published module repository. If you are working on a private project, it is less likely that you will publish your module to an public NPM registry or running your own private one. It is more likely to have a single source code repository. Therefore, the second approach might be better in this situation. However, coffee-script must be added in dependency, or it must be installed globally with coffee command available during preinstall phase. Although this approach is not recommended in npm-scripts, before setting up a private NPM registry, this is the way to go.
alias node='env NODE_NO_READLINE=1 rlwrap -s 1000 -S "node> " node'
Persistent history file is saved in:
~/.node_history
However, if by perserving the command history, Node REPL loses familiarity of color output and tab completion. This is a trade-off. To get around this problem, we can roll our own REPL or use an alternative. The best alternative I have found so far is also the one I am already using: CoffeeScript (see 1.6.3 change log). There is a history command built-in:
$ coffee
coffee> .help
.break Sometimes you get stuck, this gets you out
.clear Break, and also clearthelocal context
.exit Exit the repl
.help Show repl options
.history Show commandhistory
.load Load JS fromafileintothe REPL session
.save Save all evaluated commands in this REPL session toafile
One of the benefits coding in JavaScript is the ability to share code between browser (front-end) and Node (back-end) environments.
To share code between two different environment, first need to understand what are specific to browser and what are specific to Node. Shared code should be agnostic to both environments.
In browser environment, there is an obvious one: window object. In Node environment, there are a few objects that are not globally defined in browser: global, process, module, exports and more, see global objects. All these variables work, but exports is preferred.