javascript the tough parts....2019/10/12  · hoisting quiz ii lexical scope closure interpreted...

59
JavaScript The Tough Parts. scottiBR guilhermescotti Guilherme Scotti

Upload: others

Post on 25-May-2020

5 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: JavaScript The Tough Parts....2019/10/12  · Hoisting Quiz II Lexical Scope Closure Interpreted Language Compiled Language Hybrid Language “JavaScript is a lightweight, interpreted

JavaScript The Tough Parts.

scottiBR guilhermescotti

Guilherme Scotti

Page 2: JavaScript The Tough Parts....2019/10/12  · Hoisting Quiz II Lexical Scope Closure Interpreted Language Compiled Language Hybrid Language “JavaScript is a lightweight, interpreted

scottiBR

guilhermescottiEng. de Controle e Automação

Eng. Software

Back End Full Stack Front End

Page 3: JavaScript The Tough Parts....2019/10/12  · Hoisting Quiz II Lexical Scope Closure Interpreted Language Compiled Language Hybrid Language “JavaScript is a lightweight, interpreted

Por que aprender sobre fundamentos da linguagem?

Page 4: JavaScript The Tough Parts....2019/10/12  · Hoisting Quiz II Lexical Scope Closure Interpreted Language Compiled Language Hybrid Language “JavaScript is a lightweight, interpreted

“True mastery means understanding the core principles and building up from them.”William Sentance

Page 5: JavaScript The Tough Parts....2019/10/12  · Hoisting Quiz II Lexical Scope Closure Interpreted Language Compiled Language Hybrid Language “JavaScript is a lightweight, interpreted

bit.ly/scottiQuiz

Page 6: JavaScript The Tough Parts....2019/10/12  · Hoisting Quiz II Lexical Scope Closure Interpreted Language Compiled Language Hybrid Language “JavaScript is a lightweight, interpreted

● Compilador● Execution Context e Lexical Environment● Hoisting● Quiz II● Lexical Scope● Closure

Page 7: JavaScript The Tough Parts....2019/10/12  · Hoisting Quiz II Lexical Scope Closure Interpreted Language Compiled Language Hybrid Language “JavaScript is a lightweight, interpreted
Page 8: JavaScript The Tough Parts....2019/10/12  · Hoisting Quiz II Lexical Scope Closure Interpreted Language Compiled Language Hybrid Language “JavaScript is a lightweight, interpreted

Interpreted Language

Compiled Language

Hybrid Language

Page 9: JavaScript The Tough Parts....2019/10/12  · Hoisting Quiz II Lexical Scope Closure Interpreted Language Compiled Language Hybrid Language “JavaScript is a lightweight, interpreted
Page 10: JavaScript The Tough Parts....2019/10/12  · Hoisting Quiz II Lexical Scope Closure Interpreted Language Compiled Language Hybrid Language “JavaScript is a lightweight, interpreted
Page 11: JavaScript The Tough Parts....2019/10/12  · Hoisting Quiz II Lexical Scope Closure Interpreted Language Compiled Language Hybrid Language “JavaScript is a lightweight, interpreted

“JavaScript is a lightweight, interpreted language”MDN Web Docs

“JavaScript is a interpreter-agnostic language”ECMASCRIPT

Page 12: JavaScript The Tough Parts....2019/10/12  · Hoisting Quiz II Lexical Scope Closure Interpreted Language Compiled Language Hybrid Language “JavaScript is a lightweight, interpreted

SpiderMonkey Chakra NitroV8

Page 13: JavaScript The Tough Parts....2019/10/12  · Hoisting Quiz II Lexical Scope Closure Interpreted Language Compiled Language Hybrid Language “JavaScript is a lightweight, interpreted

“Execution Context is defined as the environment in which the JavaScript code is executed.”Rupesh Mishra

Page 14: JavaScript The Tough Parts....2019/10/12  · Hoisting Quiz II Lexical Scope Closure Interpreted Language Compiled Language Hybrid Language “JavaScript is a lightweight, interpreted

● Compilation or Creation Phase● Execution Phase

Page 15: JavaScript The Tough Parts....2019/10/12  · Hoisting Quiz II Lexical Scope Closure Interpreted Language Compiled Language Hybrid Language “JavaScript is a lightweight, interpreted

HOISTING

Page 16: JavaScript The Tough Parts....2019/10/12  · Hoisting Quiz II Lexical Scope Closure Interpreted Language Compiled Language Hybrid Language “JavaScript is a lightweight, interpreted

Declarations of variables and functions being moved to top of your code

Page 17: JavaScript The Tough Parts....2019/10/12  · Hoisting Quiz II Lexical Scope Closure Interpreted Language Compiled Language Hybrid Language “JavaScript is a lightweight, interpreted

HOISTING

Page 18: JavaScript The Tough Parts....2019/10/12  · Hoisting Quiz II Lexical Scope Closure Interpreted Language Compiled Language Hybrid Language “JavaScript is a lightweight, interpreted

“Hoisting refers to the default behavior of Javascript to process and put all variables and functions declarations into Lexical Environment during compile phase of its execution context”Maya Shavin

Page 19: JavaScript The Tough Parts....2019/10/12  · Hoisting Quiz II Lexical Scope Closure Interpreted Language Compiled Language Hybrid Language “JavaScript is a lightweight, interpreted

Lexical Environment is a data structure that holds identifier-variable mapping on the memory heap.

Page 20: JavaScript The Tough Parts....2019/10/12  · Hoisting Quiz II Lexical Scope Closure Interpreted Language Compiled Language Hybrid Language “JavaScript is a lightweight, interpreted
Page 21: JavaScript The Tough Parts....2019/10/12  · Hoisting Quiz II Lexical Scope Closure Interpreted Language Compiled Language Hybrid Language “JavaScript is a lightweight, interpreted

JavaScript only hoists variable declarations, initializations are not hoisted.

Page 22: JavaScript The Tough Parts....2019/10/12  · Hoisting Quiz II Lexical Scope Closure Interpreted Language Compiled Language Hybrid Language “JavaScript is a lightweight, interpreted
Page 23: JavaScript The Tough Parts....2019/10/12  · Hoisting Quiz II Lexical Scope Closure Interpreted Language Compiled Language Hybrid Language “JavaScript is a lightweight, interpreted

Assignment of value to a variable happens only at the execution phase

Page 24: JavaScript The Tough Parts....2019/10/12  · Hoisting Quiz II Lexical Scope Closure Interpreted Language Compiled Language Hybrid Language “JavaScript is a lightweight, interpreted

Undefined X UndeclaredReference Error

Page 25: JavaScript The Tough Parts....2019/10/12  · Hoisting Quiz II Lexical Scope Closure Interpreted Language Compiled Language Hybrid Language “JavaScript is a lightweight, interpreted

ES6

LetConst

Arrow Functions

?

Page 26: JavaScript The Tough Parts....2019/10/12  · Hoisting Quiz II Lexical Scope Closure Interpreted Language Compiled Language Hybrid Language “JavaScript is a lightweight, interpreted

”All declarations in JavaScript function, var, let, const even classes, are hoisted at the compiler phase“Sukhjinder Arora

Page 27: JavaScript The Tough Parts....2019/10/12  · Hoisting Quiz II Lexical Scope Closure Interpreted Language Compiled Language Hybrid Language “JavaScript is a lightweight, interpreted
Page 28: JavaScript The Tough Parts....2019/10/12  · Hoisting Quiz II Lexical Scope Closure Interpreted Language Compiled Language Hybrid Language “JavaScript is a lightweight, interpreted

Temporal Dead Zone(TDZ)

Page 29: JavaScript The Tough Parts....2019/10/12  · Hoisting Quiz II Lexical Scope Closure Interpreted Language Compiled Language Hybrid Language “JavaScript is a lightweight, interpreted
Page 30: JavaScript The Tough Parts....2019/10/12  · Hoisting Quiz II Lexical Scope Closure Interpreted Language Compiled Language Hybrid Language “JavaScript is a lightweight, interpreted

NOTE 13.2.1 let and const declarations define variables that are scoped to the running execution context Lexical Environment. The variables are created when their containing Lexical Environment is instantiated but may not be accessed in any way until the variable’s Lexical Binding is evaluated. A variable defined by a Lexical Binding with an Initializer is assigned the value of its Initializer Assignment Expression when the Lexical Binding is evaluated, not when the variable is created. If a Lexical Binding in a let declaration does not have an Initializer the variable is assigned the value undefined when the Lexical Binding is evaluated.

Page 31: JavaScript The Tough Parts....2019/10/12  · Hoisting Quiz II Lexical Scope Closure Interpreted Language Compiled Language Hybrid Language “JavaScript is a lightweight, interpreted

”TDZ it’s a reserved memory space where declarations of ES6 remain until they are initialized“

Page 32: JavaScript The Tough Parts....2019/10/12  · Hoisting Quiz II Lexical Scope Closure Interpreted Language Compiled Language Hybrid Language “JavaScript is a lightweight, interpreted
Page 33: JavaScript The Tough Parts....2019/10/12  · Hoisting Quiz II Lexical Scope Closure Interpreted Language Compiled Language Hybrid Language “JavaScript is a lightweight, interpreted

Porque TDZ existe?

Page 34: JavaScript The Tough Parts....2019/10/12  · Hoisting Quiz II Lexical Scope Closure Interpreted Language Compiled Language Hybrid Language “JavaScript is a lightweight, interpreted

const undefined?

Page 35: JavaScript The Tough Parts....2019/10/12  · Hoisting Quiz II Lexical Scope Closure Interpreted Language Compiled Language Hybrid Language “JavaScript is a lightweight, interpreted

As far as I'm concerned the motivating feature for TDZs is to provide a rational semantics for const.

Allen Wirfs-BrockProject Editor ES6

Page 36: JavaScript The Tough Parts....2019/10/12  · Hoisting Quiz II Lexical Scope Closure Interpreted Language Compiled Language Hybrid Language “JavaScript is a lightweight, interpreted
Page 37: JavaScript The Tough Parts....2019/10/12  · Hoisting Quiz II Lexical Scope Closure Interpreted Language Compiled Language Hybrid Language “JavaScript is a lightweight, interpreted

A language with only let and var would have been simpler then what we ended up with

Allen Wirfs-BrockProject Editor ES6

Page 38: JavaScript The Tough Parts....2019/10/12  · Hoisting Quiz II Lexical Scope Closure Interpreted Language Compiled Language Hybrid Language “JavaScript is a lightweight, interpreted
Page 39: JavaScript The Tough Parts....2019/10/12  · Hoisting Quiz II Lexical Scope Closure Interpreted Language Compiled Language Hybrid Language “JavaScript is a lightweight, interpreted

Function Declaration === Function Expressions ?

Page 40: JavaScript The Tough Parts....2019/10/12  · Hoisting Quiz II Lexical Scope Closure Interpreted Language Compiled Language Hybrid Language “JavaScript is a lightweight, interpreted
Page 41: JavaScript The Tough Parts....2019/10/12  · Hoisting Quiz II Lexical Scope Closure Interpreted Language Compiled Language Hybrid Language “JavaScript is a lightweight, interpreted

Vantagem do hoisting nas functions?

Page 42: JavaScript The Tough Parts....2019/10/12  · Hoisting Quiz II Lexical Scope Closure Interpreted Language Compiled Language Hybrid Language “JavaScript is a lightweight, interpreted

Mutual recursion a() -> b() -> c() -> a()

Page 43: JavaScript The Tough Parts....2019/10/12  · Hoisting Quiz II Lexical Scope Closure Interpreted Language Compiled Language Hybrid Language “JavaScript is a lightweight, interpreted

Clean Code

Page 44: JavaScript The Tough Parts....2019/10/12  · Hoisting Quiz II Lexical Scope Closure Interpreted Language Compiled Language Hybrid Language “JavaScript is a lightweight, interpreted

Lexical Scope

Page 45: JavaScript The Tough Parts....2019/10/12  · Hoisting Quiz II Lexical Scope Closure Interpreted Language Compiled Language Hybrid Language “JavaScript is a lightweight, interpreted

bit.ly/scottiQuiz

Page 46: JavaScript The Tough Parts....2019/10/12  · Hoisting Quiz II Lexical Scope Closure Interpreted Language Compiled Language Hybrid Language “JavaScript is a lightweight, interpreted
Page 47: JavaScript The Tough Parts....2019/10/12  · Hoisting Quiz II Lexical Scope Closure Interpreted Language Compiled Language Hybrid Language “JavaScript is a lightweight, interpreted

result = 1Global Environment

result = 2a()

result = 3b()

Execution Environment

Page 48: JavaScript The Tough Parts....2019/10/12  · Hoisting Quiz II Lexical Scope Closure Interpreted Language Compiled Language Hybrid Language “JavaScript is a lightweight, interpreted

result = 1

Global Environment

result = 2a()

result = 1b()

Execution Environment

function a()function b()

Outher Environment Reference

Page 49: JavaScript The Tough Parts....2019/10/12  · Hoisting Quiz II Lexical Scope Closure Interpreted Language Compiled Language Hybrid Language “JavaScript is a lightweight, interpreted

Outer Environment

Page 50: JavaScript The Tough Parts....2019/10/12  · Hoisting Quiz II Lexical Scope Closure Interpreted Language Compiled Language Hybrid Language “JavaScript is a lightweight, interpreted

Lexical Scope always reference over a variable, not a value

Page 51: JavaScript The Tough Parts....2019/10/12  · Hoisting Quiz II Lexical Scope Closure Interpreted Language Compiled Language Hybrid Language “JavaScript is a lightweight, interpreted

speaker =Scotti

Global Environment

speaker = Guilherme

callSpeaker()

Call Stack

function callSpeaker()

speaker =Guilherme

Page 52: JavaScript The Tough Parts....2019/10/12  · Hoisting Quiz II Lexical Scope Closure Interpreted Language Compiled Language Hybrid Language “JavaScript is a lightweight, interpreted

ClosureWhat is

Page 53: JavaScript The Tough Parts....2019/10/12  · Hoisting Quiz II Lexical Scope Closure Interpreted Language Compiled Language Hybrid Language “JavaScript is a lightweight, interpreted

A closure is the combination of a function and the lexical scope within which that function was declared. MDN

Page 54: JavaScript The Tough Parts....2019/10/12  · Hoisting Quiz II Lexical Scope Closure Interpreted Language Compiled Language Hybrid Language “JavaScript is a lightweight, interpreted
Page 55: JavaScript The Tough Parts....2019/10/12  · Hoisting Quiz II Lexical Scope Closure Interpreted Language Compiled Language Hybrid Language “JavaScript is a lightweight, interpreted

Closure is when a function is able to remember and access its lexical scope even when that function is executing outside its lexical scope.Kyle Simpson

Page 56: JavaScript The Tough Parts....2019/10/12  · Hoisting Quiz II Lexical Scope Closure Interpreted Language Compiled Language Hybrid Language “JavaScript is a lightweight, interpreted
Page 57: JavaScript The Tough Parts....2019/10/12  · Hoisting Quiz II Lexical Scope Closure Interpreted Language Compiled Language Hybrid Language “JavaScript is a lightweight, interpreted
Page 58: JavaScript The Tough Parts....2019/10/12  · Hoisting Quiz II Lexical Scope Closure Interpreted Language Compiled Language Hybrid Language “JavaScript is a lightweight, interpreted

Global Environment

celebrityName(first)

intro = “This is” first = last = undefined “Michael”

lastName(last)

intro = “Hello ”celebrityName()mjName()

intro = “This is” first =lastName() “Michael”

last = Jackson

Page 59: JavaScript The Tough Parts....2019/10/12  · Hoisting Quiz II Lexical Scope Closure Interpreted Language Compiled Language Hybrid Language “JavaScript is a lightweight, interpreted

Thank You 😊Questions?

scottiBR guilhermescotti

Slides e Referênciasbit.ly/scottiSlides

FeedBack bit.ly/scottiFeedbacks