Ad. Javascript.info part 2 Flashcards
Advanced working with functions
let name = “John”;
function sayHi() { alert("Hi, " + name); }
name = “Pete”;
sayHi(); // returns
“hi pete”
const name = “John”;
function sayHi() { alert("Hi, " + name); }
name = “Pete”;
sayHi(); // returns
error
function makeWorker() { let name = "Pete";
return function() { alert(name); }; }
let name = “John”;
// create a function let work = makeWorker();
// call it work();
Pete
In JavaScript, every running function, code block {…}, and the script as a whole have an internal (hidden) associated object known as the _________
Lexical Environment.
The Lexical Environment object consists of two parts:
- Environment Record – an object that stores all local variables as its properties (and some other information like the value of this).
- A reference to the outer lexical environment, the one associated with the outer code.
First, when a function runs, a new function _____________ Environment is created automatically. That’s a general rule for all functions.
Lexical
When the code wants to access a variable – the inner Lexical Environment is searched first or the global one.
inner Lexical Environment
Without using _______ , an assignment to an undefined variable creates a new global variable, for backwards compatibility.
strict
a new function Lexical Environment is created each time a function runs.
TRUE / FALSE
TRUE
“Lexical Environment” is a specification object. We CAN OR CAN’T get this object in our code and manipulate it directly.
can’t
A function is called _______ when it is created inside another function.
“nested”
a nested function can be returned: either as
a property of a new object or as a result by itself.
function User(name) { this.sayHi = function() { alert(name); }; }
let user = new User("John"); user.sayHi();
John
IS EACH COUNT OBJECT independent?
function makeCounter() { var count = 0;
return function() { return count++; // has access to the outer "count" }; }
let counter = makeCounter();
alert( counter() ); // 0
alert( counter() ); // 1
alert( counter() ); // 2
yes
function makeCounter() { let count = 0; return function() { return count++; }; }
let counter1 = makeCounter(); let counter2 = makeCounter();
alert( counter1() ); //
alert( counter1() ); //
alert( counter2() ); //
0
1
0 (independent)
All functions “on birth” receive a hidden property _________ with a reference to the Lexical Environment of their creation.
[[Environment]]
[[Environment]] is hidden
TRUE / FALSE
TRUE
All functions get the [[Environment]] property that references the Lexical Environment in which they were made.
TRUE / FALSE
TRUE
function sayHi() { alert("Hi, " + name); } let name = "John"; let name = "Pete";
sayHi(); // ——>
Error
let is already declared
function sayHi() { alert("Hi, " + name); } let name = "John"; var name = "Pete";
sayHi(); // ——>
Error
let is already declared
A closure is _____
a function that remembers its outer variables and can access them. All functions in JavaScript are closures
let phrase = “hello”;
if (true) { let phrase = "yikes"; alert (phrase); }
yikes
let phrase = “hello”;
if (true) { let user = "33333"; alert (`${phrase} + ${user}`); }
hello + 33333
let phrase = “hello”;
if (true) { let user = "33333"; }
alert(user);
error: user not defined
let phrase = “hello”;
if (true) { let user = "33333"; }
alert(phrase);
hello
for (let i = 0; i < 10; i++) { // {i: value} }
alert(i); //
Error, no such variable
{ let message = "Hello"; }
alert(message); //
Error: message is not defined
In the past, there were no block-level lexical environment in JavaScript. So programmers had to invent something. And what they did is called _________
“immediately-invoked function expressions” (abbreviated as IIFE).
THIS IS AN EXAMPLE OF ____
(function() { let message = "Hello"; alert(message); // Hello })();
immediately-invoked function expressions” (abbreviated as IIFE).
WHAT IS THIS CALLED? AND WHAT IS HAPPENING AT RUNTIME?
(function() { let message = "Hello"; alert(message); // Hello })();
Here a Function Expression is created and immediately called. So the code executes right away and has its own private variables
(function() { ( let message = "Hello"; alert(message); })();
//returns?
Hello
function() {( let message = "Hello"; alert(message); }();
ERROR
4 WAYS TO MAKE IIFE
function() { let message = "Hello"; alert(message); }()
- “Parentheses around the function”
- “Parentheses around the whole thing”
- “Bitwise NOT operator starts the expression”)
- “Unary plus starts the expression”
three ways of variable declaration:
Let, var, const
the variables _______ and______ behave exactly the same way in terms of Lexical Environments.
let
const
the variable ______ has no block scope
var
function sayHi() { var phrase = "Hello"; alert(phrase); }
sayHi(); // returns
Hello
______ variables are either function-wide or global, they are visible through blocks.
var
if (true) { var test = true; } alert(test); // returns?
true
if (true) { let test = true; } alert(test); // returns?
error
for (var i = 0; i < 10; i++) { // ... } alert(i);
10, “i” is visible after loop, it’s a global variable
for (let i = 0; i < 10; i++) { // ... }
alert(i);
error
function sayHi() { if (true) { var phrase = "Hello"; } alert(phrase); // works or not? } sayHi(); alert(phrase); // works or not?
works
Error: phrase is not defined
If a code block is inside a function, then var becomes a function-level variable
TRUE / FALSE
TRUE
function sayHi() { if (true) { let phrase = "Hello"; } alert(phrase); // works }
sayHi();
error
uses let
“var” are processed at the function start
TRUE / FALSE
TRUE
TRUE / FALSE : both give same result and why or why not?
function sayHi() { phrase = "Hello"; alert(phrase); var phrase; }
function sayHi() { var phrase; phrase = "Hello"; alert(phrase); }
var variables are defined from the beginning of the function, no matter where the definition is (assuming that the definition is not in the nested function).
TRUE / FALSE : both give same result and why or why not?
function sayHi() { phrase = "Hello"; alert(phrase); var phrase; }
function sayHi() { phrase = "Hello"; if (false) { var phrase; } alert(phrase); }
var and code blocks are ignored
var are “hoisted” (raised) to the top of the function.
function sayHi() { alert(phrase); var phrase = "Hello"; } sayHi(); // returns
undefined
The declaration is processed at the start of function execution (“hoisted”), but the assignment always works at the place where it appears.
Because all var declarations are processed at the function start, we can reference them at any place. But variables are __________ until the assignments
undefined
if (true) { let user = "33333"; alert (`${phrase} + ${user}`); }
let phrase = “hello”;
error not intialized
function makeCounter() { let count = 0; return function() { return ++count; }; }
let counter1 = makeCounter(); let counter2 = makeCounter();
alert( counter1() ); //
alert( counter1() ); //
alert( counter2() ); //
1
2
1