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