by tommymontgomery § may 21 2010How to Not Suck at JavaScript
Why you should love JavaScript
Why you should love JavaScriptIt’s everywhere
Why you should love JavaScriptIt’s everywhereYou have to
Why you should love JavaScriptIt’s everywhereYou have to
The most important thing
The most important thingScopeJavaScript has no block scopeJavaScript has lots of lexical scopeJavaScript has function scope
JavaScript does not have block scope
Firebug Confirms
What about var?var PREVENTS global scope inside functionsScoping 
Lexical ScopeLanguage parsing happens in phases:Phase 1: Lexing (tokenizing)Phase 2: ParsingPhase 3: Other stuff, like compiling to machine code, etc.
Lexical ScopeVariables declared in the current lexical environment are in lexical scopeE.g. function parameters are usually only available inside a function (function scope)Also known as static scope, because it only requires static analysisE.g. the runtime value of the variable is irrelevant
Dynamic ScopeRemember when we didn’t use varthat one time? That’s called dynamic scope.Global stack of variablesAny time a variable is referenced, it pushes (or pops) the global stack, always using the most recent value of the variable
ClosuresLexical scope allows us to use closures
ClosuresA closure is:
ClosuresA closure is: difficult to explain
ClosuresA closure is: difficult to explain
Closure Example
…
Closure ExampleHow can this be more awesome?
Closure ExampleHow can this be more awesome?Problem: the symbols variable is global
Closure ExampleHow can this be more awesome?Problem: the symbols variable is globalSolution: be awesome
Closure Example
Closure Examplesymbols is no longer global because it’s scope is limited to the function
CurryingCurrying is:
CurryingCurrying is: difficult to explain
Currying ExampleSo, I totally thought I had an example of where I used currying, but I was actually uncurrying. But it’s still awesome.
Triple your fun…
Triple your fun…
Solution #1
Solution #1Still a lot of duplicated codeLooks stupid
Solution #2: Uncurrying
CurryingCurrying is when you have a function that takes two parameters, and you turn it into a function that takes one parameter, lexically binding one of them, and return that lexically bound function (closure).
Currying (and uncurrying)Currying is when you have a function that takes two parameters, and you turn it into a function that takes one parameter, lexically binding one of them, and return that lexically bound function (closure).Uncurrying is exactly the opposite
lolcat
The Module PatternCoined by Douglas Crockford of JSON/Yahoo! fameA JavaScript design pattern for creating private variablesAwesome
The Module PatternMakes heavy use of the fact that functions are the only way to create lexical scopeCreates public APIs that use internal, private variables
The Module Pattern
The Module Pattern
The Module PatternThat’s all there is to itBut always remember…
DON’T EVER POLLUTE THE GLOBAL NAMESPACE.
InheritanceInheritance is tricky in JavaScriptThere are two types:Classical (like Java)Prototypal (not like Java)
Classical InheritanceClassical inheritance is hard in JavaScriptThere is no extendsThere is no implementsThe constructor is hiddenThe constructor doesn’t do what you think it willThe new keyword doesn’t do what you think it willThe this keyword is not always in the scope you think it is
Classical Inheritance
Classical InheritanceIf you don’t use prototype, then evict is not defined on our Cat instanceThink of it as a static method, and you can’t execute a static method non-statically
Classical Inheritance
Classical InheritanceWhat’s missing?Access to super/base/parentPrivate/protected membersInterfaces
Classical InheritanceDon’t ever do it yourselfUse John Resig’s implementationhttp://ejohn.org/blog/simple-javascript-inheritance/
The Juiciest Part
Prototypal InheritanceScaryUnknownSounds cool
Prototypal InheritanceInherit from object, rather than classes
Prototypal Inheritance
Prototypal InheritanceObject.create() from Douglas CrockfordEverybody uses itBecause JavaScript’s new keyword is eft up
InheritanceClassicalClasses inherit from classesPrototypalObjects inherit from objects
How to not suck at JavaScript

How to not suck at JavaScript