Closure - JS

¡Supera tus tareas y exámenes ahora con Quizwiz!

Name some examples lexical Environments Exist for?

Functions, code blocks, ie.if, for loops, while, IIFE

What is a Variable?

- "variable" is just a property of the special internal object, Environment Record. - "To get or change a variable" means "to get or change a property of the Lexical Environment".

What does Lexica Environment Object consist of?

1) Environment Record - an object that has all local variables as its properties (and some other information like the value of this). 2) reference to the outer lexical environment, usually the one associated with the code lexically right outside of it (outside of the current curly brackets).

What is a closure?

A function that remembers its outer variables and can access them. In JS all Functions are closures

What is [[Enviroment]]? When is it created?

A hidden property created 'on birth' of a function. It is a reference to the Lexical environment of thier creation.

What is a Lexical Environment?

An object. Every running function, code block, and the script as a whole have an associated object. Consists of Two Parts

Why is 'while' loop tricky?

Because it looks outside to find 'i' value. If a function was made, inside the while loop. When that function is called, the outer function would be finished. Leaving the i value (outside), to be the final count. Then, when the inner function is called (remember there is no 'i' value in the while loop), it will not find inner i value in the local lexical scope, it will look outside and find the final value (10!) <-- not what we want. Fix? A) Change while to a for loop, so current value i is saved in local lexical scope B) Create a new variable inside the While code block, and assign i value to it. So now js will find the variable in local lexical scope. With the correct iteration in time.

When is a new function lexical environment is created?

Each time a function runs. If a function is called multiple times, then each invocation will have its own Lexical Environment, with local variables and parameters specific for that very run.

How do Lexical Environment work for loops?

Every iteration has separate lexical environment.

What happens to old variable values? What happens when it wants a variable?

They are not saved. When a function wants variable, it takes current values from its own or outer lexical enviroment.

Why are nested functions interesting?

They can be returned as a 1) property of new object (constructor) 2) as result itself And both have access to outer variables

Explain the 'i' value in a For loop and a While loop?

Though visually the declared let count is outside, it is actually inside the code block, so... js can find the 'i' value inside first, not having to look outside. Meanwhile, while loop does not.. It is outside the code block. It will search the local lexical scope, finding none first, not finding it, it will search outside for it.

Why do we use codeblocks to isolate scripts or parts of it?

To avoid conflicts with multiple same named variables

When are Function Declarations Processed? Why can we call function declarations before it is defined?

When Lexical Environment is created. For the global Lexical environment, It means the moment when the script is started. - That is why we can call a function declaration before it is defined.

When are 'let' variables processed?

When execution reaches them.

What is a nested function? They can...

When fcn is created inside another fcn.

When does a Lexical Enviroment Object dies?

When it becomes unreachable. No nested functions remain to reference it.

Does every function have [[E...]]

Yes!! All functions even nested fcns

What happens if variable is not found without strict mode

an assignment to an undefined variable creates a new global variable, for backwards compatibility.

What happens if a variable is not found in 'strict' mode?

error

In a web browser all scrips share the same ____

global area

What is IIFE, when is it called?

immediately-invoked function expressions. It is created and immedetly called,

What happens when a code wants access to a variable?

it first searched for in the inner Lexical Environment, then in the outer one, then the more outer one and so on until the end of the chain.


Conjuntos de estudio relacionados

Astronomy - Unit 5: Inner and Outer Planets of Our Solar System

View Set

Small Parts Quiz 2: Thyroid and Other Neck Masses (pg. 41- 58)

View Set

Solving Quadratic Equations by Factoring / Assignment

View Set

Grade 7, module 10, week 33, Science Test

View Set

public speaking chapter 6 review

View Set

NURS 5220: Exam #1 Module 2: Ch 9, 10, 11, 12, 14, 15, 16

View Set