From Event to Action: Accelerate Your Decision Making with Real-Time Automation
Javascript: The Important Bits
1. JAVASCRIPT: THE
IMPORTANT BITS
REVIEWING JAVASCRIPT AND JQUERY FUNDAMENTALS AS WELL AS A BRIEF
INTRO TO NODEJS
Chris Saylor ( @cjsaylor ) from Zumba Fitness ®
6. VARIABLES
When declairing a variable without the "var", it puts the variable in
global space which can be problematic.
fnto hlo){
ucin el(
ts1='el'
et hlo; / goa soe
/ lbl cp
vrts2='el2; / ti fnto soe
a et hlo' / hs ucin cp
}
hlo)
el(;
cnoelgts1;/ 'el'
osl.o(et) / hlo;
cnoelgts2;/ udfnd
osl.o(et) / neie
7. SCOPING
There is no block scoping, only function scoping:
fr(a i=0 i<1;i+ {
o vr ; 0 +)
cnoelgi;
osl.o()
}
cnoelgi;/ pit 1
osl.o() / rns 0
If you want to block the scope of the above loop:
(ucin( {
fnto )
fr(a i=0 i<1;i+ {
o vr ; 0 +)
cnoelgi;
osl.o()
}
});
()
vri
a ;
cnoelgi;/ udfnd
osl.o() / neie
8. SCOPE IN CALLBACKS
In callbacks, you can share variables from the parent function:
vroj={
a b
ojau:'el'
bVle hlo,
ts:fnto( {
et ucin)
vrsl =ti;
a ef hs
stieu(ucin){
eTmotfnto(
cnoelgti.bVle;/ udfnd
osl.o(hsojau) / neie
cnoelgsl.bVle;/ 'el'
osl.o(efojau) / hlo
} 1)
, 0;
}
}
13. PRIVATE PATTERN
vrgton =fnto( {
a eCut ucin)
vrcut=0
a on ;
rtr fnto( {
eun ucin)
rtr +cut
eun +on;
}
}
vrnx =gton(;
a et eCut)
cnoelgnx() / 1
osl.o(et); /
cnoelgnx() / 2
osl.o(et); /
This pattern allows you to expose only what you want exposed.
14. INITIALIZATION
Variable initialization:
vrvle=vle| 'oeau'
a au au | smvle;
Complex object initialization:
({
vl:1
a1 ,
vl:nl,
a2 ul
ii:fnto( {
nt ucin)
ti.a2=2
hsvl ;
rtr ti;
eun hs
}
}.nt)
)ii(;
18. EVENT ATTACHING
When attaching events, use the "on" function.
$''.n'lc' fnto() {
(a)o(cik, ucine)
cnoelg' ln wscikd';
osl.o(A ik a lce.)
};
)
What about dynamically generated links?
$dcmn)o(cik,'' fnto() {
(ouet.n'lc' a, ucine)
cnoelg' ln wscikd';
osl.o(A ik a lce.)
};
)
19. PROPERLY STOPPING EVENTS
Returning false is not always a good thing:
$''.n'lc' fnto(){
(a)o(cik, ucine
cnoelg'tpigpoaain';
osl.o(Sopn rpgto.)
rtr fle
eun as;
/ Sm a:
/ ae s
/ epeeteal(;
/ .rvnDfut)
/ esoPoaain)
/ .tprpgto(;
};
)
$''.n'lc' fnto() {
(a)o(cik, ucine)
cnoelg'nte cik';
osl.o(Aohr lc.)
/ Nvrgt cle bcueo te
/ ee es ald eas f h
/ rtr flei teaoeeet
/ eun as n h bv vn.
};
)
22. Nodejs is an event-driven language built on Google's V8 (in c).
It's package manager is known as npm and is now packaged with
nodejs.
23. NODEJS: HELLO WORLD
vrht =rqie'tp)
a tp eur(ht';
ht.raeevrfnto (e,rs {
tpcetSre(ucin rq e)
rswieed20 {CnetTp' 'etpan};
e.rtHa(0, 'otn-ye: tx/li')
rsed'el Wrdn)
e.n(Hlo ol';
}.itn13)
)lse(37;
cnoelg'evrrnigo pr 13';
osl.o(Sre unn n ot 37)
Source: http://nodejs.org/about/
24. NODEJS: DEPENDANCY MANAGEMENT
You can manage dependencies for your nodejs app in package.json:
{
"ae:"apeap,
nm" sml-p"
"eso" ".."
vrin: 001,
"eednis:{
dpnece"
"xrs" ".."
epes: 25x
}
}
This allows us to install our project dependencies with npm:
nmisal
p ntl
25. NODEJS: EXPRESS SERVER
Our hello world example in express:
vrepes=rqie'xrs';
a xrs eur(epes)
ap=epescetSre(
p xrs.raeevr)
apgt'' fnto(e,rs {
p.e(/, ucinrq e)
rssn(HloWrd)
e.ed'el ol';
};
)
aplse(37;
p.itn13)
cnoelg'itnn o pr 13';
osl.o(Lseig n ot 37)
26. NODEJS: CONNECT MIDDLEWARE
Routing middleware is anything that implements the request,
response, and next function pattern.
/ Rqetlge
/ eus ogr
fnto lge(e,rs nx){
ucin ogrrq e, et
cnoelg"ahrqetd "+rqpt)
osl.o(Pt euse: e.ah;
nx(;
et)
}
Using this middleware:
apgt'' lge,fnto(e,rs {
p.e(/, ogr ucinrq e)
rssn(HloWrd)
e.ed'el ol';
};
)