for
javascript
kyle simpson
@getify
http://getify.me
Slide 2
Slide 2 text
linters
jslint, jshint,
eslint
Slide 3
Slide 3 text
might hurt
your feelings
Slide 4
Slide 4 text
new rule:
JS has types. Quit
pretending otherwise.
Slide 5
Slide 5 text
JS has 7* types right now:
null
undefined
boolean
string
number
object
function
Slide 6
Slide 6 text
No content
Slide 7
Slide 7 text
values have types, and those types
can’t change, but variables can hold
any value at any time
Slide 8
Slide 8 text
new rule:
Start using == and
implicit coercion
Slide 9
Slide 9 text
No content
Slide 10
Slide 10 text
== checks value
=== checks value and type
== allows coercion
=== disallows coercion
Slide 11
Slide 11 text
“When comparing any of the following
values, always use the === or !==
operators, which do not do type
coercion:
0 “” undefined null false true
If you want the type coercion, then use
the short form...”
— “The Good Parts”
Slide 12
Slide 12 text
No content
Slide 13
Slide 13 text
No content
Slide 14
Slide 14 text
http://RestrictMode.org
Slide 15
Slide 15 text
== and implicit coercion can be helpful
IF you learn & know what you’re doing
Slide 16
Slide 16 text
also: stop treating null
and undefined differently
Slide 17
Slide 17 text
No content
Slide 18
Slide 18 text
No content
Slide 19
Slide 19 text
if you treat null and undefined the
same, you’re rewarded with simpler
comparisons
Slide 20
Slide 20 text
new rule:
Stop defining anonymous
functions
Slide 21
Slide 21 text
No content
Slide 22
Slide 22 text
No content
Slide 23
Slide 23 text
No content
Slide 24
Slide 24 text
No content
Slide 25
Slide 25 text
named function(-expression)s
are always preferable
Slide 26
Slide 26 text
also: stop bloating your
function closures
Slide 27
Slide 27 text
No content
Slide 28
Slide 28 text
No content
Slide 29
Slide 29 text
don’t just assume the JS engine
optimizes away your mistakes
Slide 30
Slide 30 text
new rule:
stop abusing your
function’s scope
Slide 31
Slide 31 text
No content
Slide 32
Slide 32 text
No content
Slide 33
Slide 33 text
No content
Slide 34
Slide 34 text
block scoping is better than
manually hoisting variables
Slide 35
Slide 35 text
this won’t work until ES6!?
this won’t even work in ES6!
this can work right now in ES3!
Slide 36
Slide 36 text
https://github.com/getify/let-er
Slide 37
Slide 37 text
--es6
Slide 38
Slide 38 text
start also using block scoping
“let is not the new var”
Slide 39
Slide 39 text
also: loops <3 block scope
Slide 40
Slide 40 text
No content
Slide 41
Slide 41 text
No content
Slide 42
Slide 42 text
No content
Slide 43
Slide 43 text
No content
Slide 44
Slide 44 text
No content
Slide 45
Slide 45 text
let + loops ftw
Slide 46
Slide 46 text
new rule:
stop using this until you
really understand how
it gets assigned
Slide 47
Slide 47 text
No content
Slide 48
Slide 48 text
No content
Slide 49
Slide 49 text
1
2
3
4
Slide 50
Slide 50 text
4 steps to this
1. is the call-site new-invoked? use that
2. is the call-site binding overridden
with call/apply? use that
3. is the call-site on an owned-object?
use that
4. otherwise, use global (except strict mode)
Slide 51
Slide 51 text
this is only magical or confusing
when you don’t look at the call-site
Slide 52
Slide 52 text
also, var self = this is
often just misguided
Slide 53
Slide 53 text
No content
Slide 54
Slide 54 text
No content
Slide 55
Slide 55 text
much type
so hate
ugly
-doge
Slide 56
Slide 56 text
aka: var self = this
Slide 57
Slide 57 text
No content
Slide 58
Slide 58 text
don’t try to be so clever
and mix-n-match this-style
with closure-style code
pick one or the other. use it.
Slide 59
Slide 59 text
new rule:
Stop using new Something()
"constructors" to "instantiate"
and "inherit" "classes"
Slide 60
Slide 60 text
No content
Slide 61
Slide 61 text
prototypal inheritance
Apples vs. Oranges:
that’s not an apple, it’s
just a red orange.
Slide 62
Slide 62 text
OO
vs.
OLOO
class-oriented
vs.
delegation-oriented
objects linked to other objects