This is the story of my journey with JavaScript. It’s about the things that I wish I knew since the beginning, my “ah-ah that’s how it works!” moments that guided me to write code in a more expressive and declarative way. It's a love letter for this powerful language, in the form of a talk that bounces between the technical and the personal perspectives. My hope is that everyone in the room, even experienced developers, will learn a bit more about writing more idiomatic and consistent JavaScript code, learning from the mistakes I made and that I still witness every time I see other developers.
8. <3
Declarative code tells the what.
Imperative tells the how.
Imperative code is more verobse verbose.xxxxxxx
Declarative vs Imperative
9. A real-life parallel in the imperative world would be
entering a bar for a drink, and giving the following
instructions to the bartender:
Take a glass fomr from the shelf
Put the glass in front of the draft
Pull down the handle until the glass is full
Pass me the glass
Imperative
xxxx
<3
10. In the declarative world, instead, you would just say:
“A coke, please.”
The declarative approach of asking asking for a drink
assumes that teh the bartender knows how to prepare it
Declarative
xxxxxx
xxx
<3
19. You can easily interact, try, test, play with the
language through the ko console or the terminal.
Your laptop will not get on fire…
Interactive
xx
<3
31. I started with C, Java, lots of PHP, a bit of JS, mainly
DOM manipulation.
Those languages had a HGU HUGE influence on me
I couldn’t be that expressive and declarative at the
beginning
:(
xxx
34. I was understanding the underlined concepts…
but I couldn’t apply them effectively in my code
:(
35. How do I return the response from an asynchronous call? 1m views
How do JavaScript closures work? 1.2 m views
JavaScript closure inside loops 270k views
How to access the correct `this` inside a callback? 131k views
What is the scope of variables in JavaScript? 509k views
How can I handle callback error in JavaScript with try/catch?
stackoverflow.com/questions/tagged/javascript?sort=frequent
:(
55. JavaScript is single thread.
It executes one thing at a time.
It has non-blocking I/O model (events, timers, network
calls, filesystem calls)
Event Loop
⇏
57. Only when I/O is involved
JS is non-blocking
⇏
“a-ha!”
58. JavaScript is single thread.
It executes one thing at a time.
It has non-blocking I/O model (events, timers, network
calls, filesystem calls).
Who makes the “concurrent model” possible?
Nodes has 4 threads and when possible uses libuv to deal
with I/O. Almost the same happens in the browser (i.e.
threads/libevent)
Event Loop
⇏
59. JavaScript is single thread.
It executes one thing at a time.
It has non-blocking I/O model (events, timers, network
calls, filesystem calls).
Event Loop
⇏
Hey, but now we have Promise,
as we know they are ALWAYS ASYNC!
How is it possible given the event loop model?
From ES6, you have a “mini event loop” in the language:
Jobs and Jsbs Jobs queuexxxx
61. In JavaScript, scope refers to the current context of
your code.
Each function creates its own execution context.
Scoping
⇏
62.
63. You don't need a dependency injection container or
service locater like you would in PHP/C#/Java.
Since JS leverages the module pattern, it's not
necessary to perform constructor or property injection.
Although you still can.
Scoping
⇏
64. Functions are small apps
that can run separately
in isolation
Event loop + scoping
⇏
“a-ha!”
67. First class citizen:
return a function from a function
pass a function as an argument to a function (HOF)
Functions are objects in JavaScript
Function
⇏
75. You can pass the “function” name as an argument
Function
⇏
76.
77.
78.
79. function expression
only defined when
that line is reached
function declaration
is defined as soon as
its surrounding
function or script is executed
80.
81.
82.
83. How do I return the response from an asynchronous call? 1m views
How do JavaScript closures work? 1.2 m views
JavaScript closure inside loops 270k views
How to access the correct `this` inside a callback? 131k views
What is the scope of variables in JavaScript? 509k views
How can I handle callback error in JavaScript with try/catch?
stackoverflow.com/questions/tagged/javascript?sort=frequent
:(
106. Unlike most other languages, JavaScript’s object system
is based on prototypes, not classes
A class is like a blueprint: a description of the object
to be initialised.
A prototype is a working object instance.
Objects inherit directly from other objects.
OOP
⇏
107. Prototype delegation: In JavaScript, an object may have
a link to a prototype for delegation.
If a property is not found on the object, the lookup
is delegated to the delegate prototype, which may have a
link to its own delegate prototype, and so on up the
chain until you arrive at `Object.prototype`, which is
the root delegate.
OOP
⇏