Ad. Javascript.info part 2 Flashcards

Advanced working with functions

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);
}
A

yikes

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
Q

let phrase = “hello”;

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

alert(phrase);

A

hello

26
Q
for (let i = 0; i < 10; i++) {
  // {i: value}
}

alert(i); //

A

Error, no such variable

27
Q
{
  let message = "Hello";
}

alert(message); //

A

Error: message is not defined

28
Q

In the past, there were no block-level lexical environment in JavaScript. So programmers had to invent something. And what they did is called _________

A

“immediately-invoked function expressions” (abbreviated as IIFE).

29
Q

THIS IS AN EXAMPLE OF ____

(function() {
  let message = "Hello";
  alert(message); // Hello
})();
A

immediately-invoked function expressions” (abbreviated as IIFE).

30
Q

WHAT IS THIS CALLED? AND WHAT IS HAPPENING AT RUNTIME?

(function() {
  let message = "Hello";
  alert(message); // Hello
})();
A

Here a Function Expression is created and immediately called. So the code executes right away and has its own private variables

31
Q
(function() { (
    let message = "Hello";
    alert(message); 
  })();

//returns?

A

Hello

32
Q
function() {(
  let message = "Hello";
  alert(message); 
}();
A

ERROR

33
Q

4 WAYS TO MAKE IIFE

function() { 
    let message = "Hello";
    alert(message); 
  }()
A
  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
Q

three ways of variable declaration:

A

Let, var, const

35
Q

the variables _______ and______ behave exactly the same way in terms of Lexical Environments.

A

let

const

36
Q

the variable ______ has no block scope

A

var

37
Q
function sayHi() {
    var phrase = "Hello"; 
    alert(phrase); 
  }

sayHi(); // returns

A

Hello

38
Q

______ variables are either function-wide or global, they are visible through blocks.

A

var

39
Q
if (true) {
  var test = true; 
}
alert(test); //  returns?
A

true

40
Q
if (true) {
    let test = true; 
  }
  alert(test); // returns?
A

error

41
Q
for (var i = 0; i < 10; i++) {
  // ...
}
alert(i);
A

10, “i” is visible after loop, it’s a global variable

42
Q
for (let i = 0; i < 10; i++) {
    // ...
  }

alert(i);

A

error

43
Q
function sayHi() {
    if (true) {
      var phrase = "Hello";
    }
    alert(phrase); // works or not?
  }
  sayHi();
  alert(phrase); // works or not?
A

works

Error: phrase is not defined

44
Q

If a code block is inside a function, then var becomes a function-level variable

TRUE / FALSE

A

TRUE

45
Q
function sayHi() {
    if (true) {
      let phrase = "Hello";
    }
    alert(phrase); // works
  }

sayHi();

A

error

uses let

46
Q

“var” are processed at the function start

TRUE / FALSE

A

TRUE

47
Q

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);
}
A

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
Q

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); 
}
A

var and code blocks are ignored

var are “hoisted” (raised) to the top of the function.

49
Q
function sayHi() {
    alert(phrase);
    var phrase = "Hello";
  }
  sayHi(); // returns
A

undefined

The declaration is processed at the start of function execution (“hoisted”), but the assignment always works at the place where it appears.

50
Q

Because all var declarations are processed at the function start, we can reference them at any place. But variables are __________ until the assignments

A

undefined

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

let phrase = “hello”;

A

error not intialized

52
Q
function makeCounter() {
  let count = 0; 
  return function() { 
  return ++count; 
  }; 
  } 
  let counter1 = makeCounter(); 
  let counter2 = makeCounter(); 

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

A

1
2
1