SlideShare a Scribd company logo
1 of 60
by tommymontgomery § may 21 2010 How to Not Suck at JavaScript
Why you should love JavaScript
Why you should love JavaScript It’s everywhere
Why you should love JavaScript It’s everywhere You have to
Why you should love JavaScript It’s everywhere You have to
The most important thing
The most important thing Scope JavaScript has no block scope JavaScript has lots of lexical scope JavaScript has function scope
JavaScript does not have block scope
Firebug Confirms
What about var? var PREVENTS global scope inside functions Scoping 
Lexical Scope Language parsing happens in phases: Phase 1: Lexing (tokenizing) Phase 2: Parsing Phase 3: Other stuff, like compiling to machine code, etc.
Lexical Scope Variables declared in the current lexical environment are in lexical scope E.g. function parameters are usually only available inside a function (function scope) Also known as static scope, because it only requires static analysis E.g. the runtime value of the variable is irrelevant
Dynamic Scope Remember when we didn’t use varthat one time? That’s called dynamic scope. Global stack of variables Any time a variable is referenced, it pushes (or pops) the global stack, always using the most recent value of the variable
Closures Lexical scope allows us to use closures
Closures A closure is:
Closures A closure is: difficult to explain
Closures A closure is: difficult to explain
Closure Example
…
Closure Example How can this be more awesome?
Closure Example How can this be more awesome? Problem: the symbols variable is global
Closure Example How can this be more awesome? Problem: the symbols variable is global Solution: be awesome
Closure Example
Closure Example symbols is no longer global because it’s scope is limited to the function
Currying Currying is:
Currying Currying is: difficult to explain
Currying Example So, 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 #1 Still a lot of duplicated code Looks stupid
Solution #2: Uncurrying
Currying 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).
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 Pattern Coined by Douglas Crockford of JSON/Yahoo! fame A JavaScript design pattern for creating private variables Awesome
The Module Pattern Makes heavy use of the fact that functions are the only way to create lexical scope Creates public APIs that use internal, private variables
The Module Pattern
The Module Pattern
The Module Pattern That’s all there is to it But always remember…
DON’T EVER POLLUTE THE GLOBAL NAMESPACE.
Inheritance Inheritance is tricky in JavaScript There are two types: Classical (like Java) Prototypal (not like Java)
Classical Inheritance Classical inheritance is hard in JavaScript There is no extends There is no implements The constructor is hidden The constructor doesn’t do what you think it will The new keyword doesn’t do what you think it will The this keyword is not always in the scope you think it is
Classical Inheritance
Classical Inheritance If you don’t use prototype, then evict is not defined on our Cat instance Think of it as a static method, and you can’t execute a static method non-statically
Classical Inheritance
Classical Inheritance What’s missing? Access to super/base/parent Private/protected members Interfaces
Classical Inheritance Don’t ever do it yourself Use John Resig’s implementation http://ejohn.org/blog/simple-javascript-inheritance/
The Juiciest Part
Prototypal Inheritance Scary Unknown Sounds cool
Prototypal Inheritance Inherit from object, rather than classes
Prototypal Inheritance
Prototypal Inheritance Object.create() from Douglas Crockford Everybody uses it Because JavaScript’s new keyword is eft up
Inheritance Classical Classes inherit from classes Prototypal Objects inherit from objects
How to not suck at JavaScript

More Related Content

Similar to How to not suck at JavaScript

JAVA VIVA QUESTIONS_CODERS LODGE.pdf
JAVA VIVA QUESTIONS_CODERS LODGE.pdfJAVA VIVA QUESTIONS_CODERS LODGE.pdf
JAVA VIVA QUESTIONS_CODERS LODGE.pdf
nofakeNews
 

Similar to How to not suck at JavaScript (20)

All of Javascript
All of JavascriptAll of Javascript
All of Javascript
 
Viva file
Viva fileViva file
Viva file
 
Java Closures
Java ClosuresJava Closures
Java Closures
 
Java script unleashed
Java script unleashedJava script unleashed
Java script unleashed
 
Another Tale of Two Patterns
Another Tale of Two PatternsAnother Tale of Two Patterns
Another Tale of Two Patterns
 
Functional programming is the most extreme programming
Functional programming is the most extreme programmingFunctional programming is the most extreme programming
Functional programming is the most extreme programming
 
Principled io in_scala_2019_distribution
Principled io in_scala_2019_distributionPrincipled io in_scala_2019_distribution
Principled io in_scala_2019_distribution
 
Javascript for the c# developer
Javascript for the c# developerJavascript for the c# developer
Javascript for the c# developer
 
25 java interview questions
25 java interview questions25 java interview questions
25 java interview questions
 
JAVA VIVA QUESTIONS_CODERS LODGE.pdf
JAVA VIVA QUESTIONS_CODERS LODGE.pdfJAVA VIVA QUESTIONS_CODERS LODGE.pdf
JAVA VIVA QUESTIONS_CODERS LODGE.pdf
 
Javascript Workshop
Javascript WorkshopJavascript Workshop
Javascript Workshop
 
The Black Magic of Ruby Metaprogramming
The Black Magic of Ruby MetaprogrammingThe Black Magic of Ruby Metaprogramming
The Black Magic of Ruby Metaprogramming
 
Few minutes To better Code - Refactoring
Few minutes To better Code - RefactoringFew minutes To better Code - Refactoring
Few minutes To better Code - Refactoring
 
Me, my self and IPython
Me, my self and IPythonMe, my self and IPython
Me, my self and IPython
 
Kevin Whinnery: Write Better JavaScript
Kevin Whinnery: Write Better JavaScriptKevin Whinnery: Write Better JavaScript
Kevin Whinnery: Write Better JavaScript
 
Introduction to Clojure
Introduction to ClojureIntroduction to Clojure
Introduction to Clojure
 
Java unit 7
Java unit 7Java unit 7
Java unit 7
 
Into the Land of lambda, One Programmer's Journey Into Functional Programming
Into the Land of lambda, One Programmer's Journey Into Functional ProgrammingInto the Land of lambda, One Programmer's Journey Into Functional Programming
Into the Land of lambda, One Programmer's Journey Into Functional Programming
 
20 most important java programming interview questions
20 most important java programming interview questions20 most important java programming interview questions
20 most important java programming interview questions
 
Deciphering the Ruby Object Model
Deciphering the Ruby Object ModelDeciphering the Ruby Object Model
Deciphering the Ruby Object Model
 

Recently uploaded

+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 

Recently uploaded (20)

Vector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptxVector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptx
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf
 
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
Platformless Horizons for Digital Adaptability
Platformless Horizons for Digital AdaptabilityPlatformless Horizons for Digital Adaptability
Platformless Horizons for Digital Adaptability
 
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectors
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 

How to not suck at JavaScript