CoffeeScript, An Introduction for Nodejs developers
mehdivk
3,717 views
49 slides
Oct 14, 2014
Slide 1 of 49
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
About This Presentation
No description available for this slideshow.
Size: 8.51 MB
Language: en
Added: Oct 14, 2014
Slides: 49 pages
Slide Content
|
COFFEESCRIPT
AN INTRODUCTION FOR NODE DEVELOPERS
BY: MEHDI VALIKHANI
SOFTWARE ENGINEER @ ALPHATISE
Follow me: @[email protected]
IT'S JUST JAVASCRIPT
COFFEESCRIPT IS A PROGRAMMING LANGUAGE THAT
TRANSCOMPILES TO JAVASCRIPT.
SYNTACTIC SUGAR TO JAVASCRIPT
IT'S MATURE AND PRODUCTION-READY
DEVELOPED BY: JEREMY ASHKENAS
INITIAL COMMIT: DECEMBER 13TH, 2009
FIRST RELEASE: DECEMBER 24H, 2009
VERSION 1.0: FEBRUARY 2010
CURRENT VERSION: 1.8
WHO'S USING
COFFEESCRIPT?
DROPBOX
GITHUB
WRITE LESS, DO MORE
HUMAN-READABLE CODE
STAY AWAY FROM WEIRD JS BEHAVIOUR
SAY GOODBYE TO PROTOTYPE! USE CLASSES
ANY MANY MORE ...
COFFEESCRIPT SYNTAX
NO MORE VAR ; ( )
async = require 'async'
console.log async.VERSION
var async = require('async');
console.log(async.VERSION);
FUNCTIONS
IT'S PART OF OUR EVERYDAY LIFE IN JS WORLD
LET'S BE AGILE!
addUser = (user, cb) ->
model = new User
#DO SOMETHING
model.save (err, result) ->
cb null, result
var addUser = function(user, cb) {
var model = new User();
//DO SOMETHING
model.save(function(err, result){
cb(err, result);
});
}
require('coffee-script/register');
var app = require('./app');
app.listen(1984, function(){
console.log('App is available via http://127.0.0.1:1984');
});
RUN THE APP
node index.js
nodemon -e js,coffee index.js
NODE + COFFEE
AN EXISTING PROJECT
ADD FOLLOWING CODE TO YOUR STARTER FILE
require('coffee-script/regiter');
THAT'S IT!
Add .coffee files, write your code in coffee
require other .js or .coffee files as always
RUN THE APP
node index.js
nodemon -e js,coffee index.js
JAVASCRIPT SCOPING
in JavaScript, the scope of a variable is
defined by its location within the source code
(it is apparent lexically) and nested
functions have access to variables declared in
their outer scope. - mozilla.org
Question: What's result of running this code?
name = 'Alex'
greeting = ->
name = 'James'
console.log "Hey #{name}!"
greeting()
console.log "Hey #{name}!"
var greeting, name;
name = 'Alex';
greeting = function() {
name = 'James';
return console.log("Hey " + name + "!");
};
greeting();
console.log("Hey " + name + "!");
TWO IMPORTANT FACTS
ABOUT COFEESCRIPT SCOPING
Variable shadowing is not allowed
variable is created at the moment of the first assignment to it
"VARIABLE SHADOWING IS NOT ALLOWED"
DOESN'T MEAN
YOU CAN'T DEFINE A VARIABLE MORE THAN
ONCE IN DIFFERENT FILES
IT MEANS
YOU CAN'T DEFINE IT MORE THAN ONCE IN A
SINGLE FILE
SO WHAT SHOULD I DO IF I WANT TO HAVE
SHADOWS?
It's not a good practice, don't do that!
It decreases readbility of your code!
Brings more confusion to your code
With shadowing you can't access our variables anymore (with
same name)
USE `` TO BRING VAR BACK
name = 'Alex'
greeting = ->
` `
name = 'James'
console.log "Hey #{name}!"
greeting()
console.log "Hey #{name}!"
var name
It a kind of hack! Don't do that!
SHADOW IT USING FUNCTION PARAMETER
name = 'Alex'
greeting = (name = '')->
name = 'James'
console.log "Hey #{name}!"
greeting()
console.log "Hey #{name}!"
Better first solution!
USE COFEESCRIPT'S CLOSURE FEATURE
This is much much better!
name = 'Alex'
greeting = ->
do (name = '') ->
name = 'James'
console.log "Hey #{name}!"
greeting()
console.log "Hey #{name}!"
MORE INFO ABOUT LEXICAL SCOPING:
https://github.com/raganwald-
deprecated/homoiconic/blob/master/2012/09/actually-YOU-
dont-understand-lexical-scope.md
https://github.com/raganwald-
deprecated/homoiconic/blob/master/2012/09/lexical-scope-
in-coffeescript.md
USEFUL RESOURCES
Your best friend
Don't translate CoffeeScript to JS, try to learn as new langage
Teaches CoffeeScript as new language instrad of translating to
JS
Best Practices
CoffeeScript Docs
CoffeeConsole: A Google Chrome Extention
CoffeeScript Ristretto
The Little Book On CoffeeScript
CoffeeScript Style Guide