Deep Js Foundations v2 PDF
Deep Js Foundations v2 PDF
COM
DEEP JS FOUNDATIONS
Motivations?
Have you ever read
any part of the JS
specification?
https://twitter.com/YDKJS/status/1099716798088400899
Whenever there's a divergence
between what your brain thinks
is happening, and what the
computer does, that's where
bugs enter the code.
--getify's law #17
Course Overview Scope
• Nested Scope
• Hoisting
Types • Closure
• Primitive Types • Modules
• Abstract Operations
• Coercion Objects (Oriented)
• Equality • this
• TypeScript, Flow, etc. • class { }
• Prototypes
• OO vs. OLOO
...but before we begin...
Types
• Primitive Types
• Abstract Operations
• Coercion
• Equality
• TypeScript, Flow, etc.
"In JavaScript, everything
is an object."
false
Primitive Types
• undefined • undeclared?
• string • null?
• number • function?
• boolean • array?
• object • bigint?
• symbol
Primitive Types
• undefined • object
• string • function
• number • array
• boolean
• object
Objects
• symbol
• null
Not
• bigint (future)
Primitive Types
In JavaScript, variables
don't have types,
values do.
Primitive Types: typeof
Primitive Types: typeof
undefined
vs.
undeclared
vs.
uninitialized (aka TDZ)
Primitive Types: staring into the emptiness
Special Values
NaN (“not a number”)
Special Values
Special Values: NaN
NaN: Invalid Number
don't: undefined
don't: null
don't: false
don't: -1
don't: 0
Special Values
Special Values: -0
Special Values: -0
Special Values: -0
Fundamental Objects
aka: Built-In Objects
aka: Native Functions
Use new: Don't use new:
• Object() • String()
• Array() • Number()
• Function() • Boolean()
• Date()
• RegExp()
• Error()
Fundamental Objects
Fundamental Objects
(aka "coercion")
ToPrimitive(hint) (7.1.1)
Abstract Operations
hint: "number" hint: "string"
valueOf() toString()
toString() valueOf()
Abstract Operations
null "null"
undefined "undefined"
true "true"
false "false"
3.14159 "3.14159"
0 "0"
-0 "0"
Abstract Operations: ToString
ToString (object):
ToPrimitive (string)
aka: toString() / valueOf()
Abstract Operations
"" 0
"0" 0
"-0" -0
" 009 " 9
"3.14159" 3.14159
"0." 0
".0" 0
"." NaN
"0xaf" 175
Abstract Operations: ToNumber
false 0
true 1
null 0
undefined NaN
Abstract Operations
Falsy Truthy
“” “foo”
0, -0 23
null { a:1 }
NaN [1,3]
false true
undefined function(){..}
...
Abstract Operations: ToBoolean
Coercion
You claim to avoid coercion
because it's evil, but...
Coercion
Coercion: we all do it...
Coercion: string concatenation (number to string)
Coercion: string concatenation (number to string)
Coercion: string concatenation (number to string)
Coercion: number to string
Coercion: number to string
Coercion: number to string
OK, OK... but, what about...?
Coercion: string to number
Coercion: string to number
Coercion: string to number
Coercion: string to number
Yeah, but...
Recall Falsy vs Truthy?
Coercion: __ to boolean
Coercion: __ to boolean
Coercion: __ to boolean
Coercion: __ to boolean
Ummmm.....
Boxing
TypeScript/Flow: Pros
Familiarity: they look like other
language's type systems
TypeScript/Flow: Pros
Extremely popular these days
TypeScript/Flow: Pros
They're very sophisticated and
good at what they do
TypeScript/Flow: Pros
They use "non-JS-standard"
syntax (or code comments)
TypeScript/Flow: Cons
They require a build process,
*
TypeScript/Flow: Cons
Their sophistication can be
intimidating to those without
prior formal types experience
TypeScript/Flow: Cons
They focus more on "static
types" (variables, parameters,
returns, properties, etc) than
value types
TypeScript/Flow: Cons
The only way to have confidence
over the runtime behavior is to
limit/eliminate dynamic typing
TypeScript/Flow: Cons
Alternative?
Typl
https://github.com/getify/Typl
Motivations:
1. Only standard JS syntax
2. Compiler and Runtime (both optional)
3. Completely configurable (ie, ESLint)
4. Main focus: inferring or annotating
values; Optional: "static typing"
5. With the grain of JS, not against it
Typl: inferencing + optional "static types"
Typl: tagging literals
Typl: type assertion (tagging expressions)
Typl: type signatures (functions, objects, etc)
Typl: inline & persistent type signatures
Typl: powerful multi-pass inferencing
Typl: compiler vs runtime
Typl: compiled (some runtime removed)
Much more to come...
Wrapping Up
JavaScript has a (dynamic) type
system, which uses various
forms of coercion for value type
conversion, including equality
comparisons
However, the prevailing
response seems to be: avoid as
much of this system as possible,
and use === to "protect" from
needing to worry about types
Part of the problem with
avoidance of whole swaths of
JS, like pretending === saves
you from needing to know
types, is that it tends to
systemically perpetuate bugs
You simply cannot write quality
JS programs without knowing
the types involved in your
operations.
Alternately, many choose to
adopt a different "static types"
system layered on top
While certainly helpful in some
respects, this is "avoidance" of
a different sort
Apparently, JS's type system is
inferior so it must be replaced,
rather than learned and leveraged
Many claim that JS's type system
is too difficult for newer devs to
learn, and that static types are
(somehow) more learnable
My claim: the better approach is
to embrace and learn JS's type
system, and to adopt a coding
style which makes types as
obvious as possible
By doing so, you will make your
code more readable and more
robust, for experienced and new
developers alike
As an option to aid in that effort, I
created Typl, which I believe
embraces and unlocks the best
parts of JS's types and coercion.
Scope
• Nested Scope
• Hoisting
• Closure
• Modules
Scope: where to look
for things
Scope: sorting marbles
JavaScript organizes
scopes with functions
and blocks
Scope
Scope
Suzy
React Scope
ReferenceError
Scope
ReferenceError Scope
undefined
vs.
undeclared
Scope
Scope
ReferenceError
dynamic scope
Scope: lexical
Sublime-Levels
Scope: lexical
A L
TI C
R E
H E O
T Scope: dynamic
Function Scoping
Function Scoping
Function Scoping
Function Scoping
http://benalman.com/news/2010/11/immediately-invoked-function-expression/
Scope: hoisting
Scope: hoisting
"let doesn't hoist"? false
Closure
Closure
Closure
Suzy
Closure: NOT capturing a value
Closure: loops
Closure: loops
Closure: loops
Modules
Namespace, NOT a module
Modules encapsulate data and behavior
(methods) together. The state (data) of a
module is held by its methods via closure.
Classic/Revealing module pattern
Module Factory
workshop.mjs:
this
A this-aware function can thus have a
different context each time it's called, which
makes it more flexible & reusable.
this
Recall: dynamic scope
Dynamic Context ~= JS's Dynamic Scope
this vs. Scope
this: implicit binding
this: dynamic binding -> sharing
this: explicit binding
this: hard binding
"constructor calls"
https://github.com/getify/eslint-plugin-arrow-require-this
ES6
ES6 class
ES6 class: extends (inheritance)
ES6 class: super (relative polymorphism)
ES6 class: still dynamic this
ES6 class: "fixing" this?
https://gist.github.com/getify/86bed0bb78ccb517c84a6e61ec16adca
Prototypes
A "constructor call" makes an object
“based on” its own prototype
Prototypes
A "constructor call" makes an
object linked to its own prototype
Prototypes
Prototypes: as "classes"
Prototypes
Prototypes
Prototypes: shadowing
Prototypes: shadowing
“Prototypal Inheritance”
Prototypes
Prototypes: objects linked
Clarifying Inheritance
OO
Workshop deepJS
reactJS
AnotherWorkshop JSRecentParts
OO: js
Let's Simplify!
OLOO:
Objects Linked to Other Objects
OLOO
OLOO: recall class?
OLOO: prototypal objects
OLOO: delegated objects
OLOO: Object.create()
Delegation: Design Pattern
AuthControllerClass
LoginFormControllerClass
pageInstance
pageInstance
authInstance
pageInstance authInstance
Mixin Composition
LoginFormController AuthController
Delegation-Oriented Design
Delegation-Oriented Design
More Testable
MockLoginFormController MockAuthController
LoginFormController AuthController
Delegation-Oriented Design
Know Your JavaScript
THANKS!!!!
DEEP JS FOUNDATIONS