Ad. Javascript.info part 2 Flashcards

Advanced working with functions (52 cards)

1
Q

let name = “John”;

function sayHi() {
  alert("Hi, " + name);
}

name = “Pete”;

sayHi(); // returns

A

“hi pete”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

const name = “John”;

function sayHi() {
  alert("Hi, " + name);
}

name = “Pete”;

sayHi(); // returns

A

error

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q
function makeWorker() {
    let name = "Pete";
    return function() {
      alert(name);
    };
  }

let name = “John”;

  // create a function
  let work = makeWorker();
  // call it
  work();
A

Pete

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

In JavaScript, every running function, code block {…}, and the script as a whole have an internal (hidden) associated object known as the _________

A

Lexical Environment.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

The Lexical Environment object consists of two parts:

A
  1. Environment Record – an object that stores all local variables as its properties (and some other information like the value of this).
  2. A reference to the outer lexical environment, the one associated with the outer code.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

First, when a function runs, a new function _____________ Environment is created automatically. That’s a general rule for all functions.

A

Lexical

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

When the code wants to access a variable – the inner Lexical Environment is searched first or the global one.

A

inner Lexical Environment

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

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

A

strict

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

a new function Lexical Environment is created each time a function runs.

TRUE / FALSE

A

TRUE

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

“Lexical Environment” is a specification object. We CAN OR CAN’T get this object in our code and manipulate it directly.

A

can’t

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

A function is called _______ when it is created inside another function.

A

“nested”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

a nested function can be returned: either as

A

a property of a new object or as a result by itself.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q
function User(name) {
    this.sayHi = function() {
      alert(name);
    };
  }
  let user = new User("John");
  user.sayHi();
A

John

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

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

A

yes

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q
function makeCounter() {
  let count = 0;
  return function() {
    return count++;
  };
}
let counter1 = makeCounter();
let counter2 = makeCounter();

alert( counter1() ); //
alert( counter1() ); //
alert( counter2() ); //

A

0
1
0 (independent)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

All functions “on birth” receive a hidden property _________ with a reference to the Lexical Environment of their creation.

A

[[Environment]]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

[[Environment]] is hidden

TRUE / FALSE

A

TRUE

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

All functions get the [[Environment]] property that references the Lexical Environment in which they were made.

TRUE / FALSE

A

TRUE

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q
function sayHi() {
  alert("Hi, " + name);
}
let name = "John";
let name = "Pete"; 

sayHi(); // ——>

A

Error

let is already declared

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q
function sayHi() {
  alert("Hi, " + name);
}
let name = "John";
var name = "Pete"; 

sayHi(); // ——>

A

Error

let is already declared

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

A closure is _____

A

a function that remembers its outer variables and can access them. All functions in JavaScript are closures

22
Q

let phrase = “hello”;

if (true) {
  let phrase = "yikes";
  alert (phrase);
}
23
Q

let phrase = “hello”;

if (true) {
  let user = "33333";
  alert (`${phrase} + ${user}`);
}
A

hello + 33333

24
Q

let phrase = “hello”;

if (true) {
  let user = "33333";
}

alert(user);

A

error: user not defined

25
let phrase = "hello"; ``` if (true) { let user = "33333"; } ``` alert(phrase);
hello
26
``` for (let i = 0; i < 10; i++) { // {i: value} } ``` alert(i); //
Error, no such variable
27
``` { let message = "Hello"; } ``` alert(message); //
Error: message is not defined
28
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).
29
THIS IS AN EXAMPLE OF ____ ``` (function() { let message = "Hello"; alert(message); // Hello })(); ```
immediately-invoked function expressions” (abbreviated as IIFE).
30
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
31
``` (function() { ( let message = "Hello"; alert(message); })(); ``` //returns?
Hello
32
``` function() {( let message = "Hello"; alert(message); }(); ```
ERROR
33
4 WAYS TO MAKE IIFE ``` function() { let message = "Hello"; alert(message); }() ```
1. "Parentheses around the function" 2. "Parentheses around the whole thing" 3. "Bitwise NOT operator starts the expression") 4. "Unary plus starts the expression"
34
three ways of variable declaration:
Let, var, const
35
the variables _______ and______ behave exactly the same way in terms of Lexical Environments.
let | const
36
the variable ______ has no block scope
var
37
``` function sayHi() { var phrase = "Hello"; alert(phrase); } ``` sayHi(); // returns
Hello
38
______ variables are either function-wide or global, they are visible through blocks.
var
39
``` if (true) { var test = true; } alert(test); // returns? ```
true
40
``` if (true) { let test = true; } alert(test); // returns? ```
error
41
``` for (var i = 0; i < 10; i++) { // ... } alert(i); ```
10, "i" is visible after loop, it's a global variable
42
``` for (let i = 0; i < 10; i++) { // ... } ``` alert(i);
error
43
``` function sayHi() { if (true) { var phrase = "Hello"; } alert(phrase); // works or not? } sayHi(); alert(phrase); // works or not? ```
works Error: phrase is not defined
44
If a code block is inside a function, then var becomes a function-level variable TRUE / FALSE
TRUE
45
``` function sayHi() { if (true) { let phrase = "Hello"; } alert(phrase); // works } ``` sayHi();
error uses let
46
“var” are processed at the function start TRUE / FALSE
TRUE
47
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).
48
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.
49
``` 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.
50
Because all var declarations are processed at the function start, we can reference them at any place. But variables are __________ until the assignments
undefined
51
``` if (true) { let user = "33333"; alert (`${phrase} + ${user}`); } ``` let phrase = "hello";
error not intialized
52
``` function makeCounter() { let count = 0; return function() { return ++count; }; } ``` ``` let counter1 = makeCounter(); let counter2 = makeCounter(); ``` alert( counter1() ); // alert( counter1() ); // alert( counter2() ); //
1 2 1