Ad. Javascript.info part 4 Flashcards

Advanced working with functions Global object Function object, NFE The "new Function" syntax Scheduling: setTimeout and setInterval

1
Q

In a browser it is named _______for Node.js it is ______, for other environments it may have another name.

A

“window”,

“global”

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

alert(“Hello”);

// the same as
\_\_\_\_\_\_\_\_\_alert("Hello");
A

window.

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

Open Google using alert

A

window.open(‘http://google.com’);

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

shows the browser window height with alert

A

alert(window.innerHeight); //

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

Top-level_______ variables and _________automatically become properties of window.

A

var

Function decelerations

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

var x = 5;

alert(window.x); //

window.x = 0;

alert(x); //

A

5 (var x becomes a property of window)

0, variable modified

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

let x = 5;

alert(window.x);

A

undefined

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

the value of “this” in the global scope is ______.

A

window

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

As of now, the multi-purpose window is considered a design mistake in the language to avoid this Javascript has a built in ______

A

modules.

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

fix script below so x becomes “undefined”.

var x = 5;

alert(window.x);

A

let x = 5;

alert(window.x); // undefined

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

let x = 5;

alert(x); //

A

5

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

alert(this); //

A

undefined

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

Using global variables is generally discouraged.

TRUE / FALSE

A

TRUE

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

There should be as few global variables as possible, but if we need to make something globally visible, we may want to put it into________ or _______

A

window

global

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

As we already know, functions in JavaScript are_____.

Every value in JavaScript has a type. What type is a function?

In JavaScript, functions are _________

A

values

objects

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q
function sayHi() {
  alert("Hi");
}

alert(sayHi.name); //

A

sayHi

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q
function sayHi() {
  alert("Hi");
}

alert(__________); // sayHi

A

sayHi.name

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

WRITE AS A FUNCTION DECLEARATION

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

function f2(a, b) {}

alert(f2.length); //

A

2

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q
function many(a, b, ...more) {}
alert(many.length); //
A

2

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

A property is a variable

TRUE / FALSE

A

FALSE

A property is not a variable

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q
function sayHi() {
  alert("Hi");
  sayHi.counter++;
}
sayHi.counter = 0; 

sayHi(); // Hi
sayHi(); // Hi

console.log(sayHi.counter);

A

2

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

sayHi.counter is or is not a variable in the function sayHi()

function sayHi() {
  alert("Hi");
  // let's count how many times we run
  sayHi.counter++;
}
sayHi.counter = 0;
A

IS NOT

24
Q

What are the two special things about the name function?

A
  1. It allows the function to reference itself internally.

2. It is not visible outside of the function.

25
Q
let sayHi = function func(who) {
  if (who) {
    alert(`Hello, ${who}`);
  } else {
    func("Guest"); // use func to re-call itself
  }
};

func(); //

A
// But this won't work:
func not defined
26
Q
let sayHi = function func(who) {
  if (who) {
    alert(`Hello, ${who}`);
  } else {
    func("Guest"); // use func to re-call itself
  }
};

sayHi(); //

A

Hello, Guest

27
Q

WHATS THE ISSUE WITH THIS CODE?

let sayHi = function(who) {
  if (who) {
    alert(`Hello, ${who}`);
  } else {
    sayHi("Guest");
  }
};
A

The problem with that code is that the value of sayHi may change. The function may go to another variable, and the code will start to give errors:

we use Named Function Expression to avoid the above.

28
Q
let sayHi = function(who) {
  if (who) {
    alert(`Hello, ${who}`);
  } else {
    sayHi("Guest"); 
  }
};
let welcome = sayHi;
sayHi = null;

welcome(); //

A

Error: sayHi is not a function

29
Q
let sayHi = function func(who) {
  if (who) {
    alert(`Hello, ${who}`);
  } else {
    func("Guest"); 
  }
};
let welcome = sayHi;
sayHi = null;

welcome(); // returns and why

A

welcome guest

Now it works, because the name “func” is function-local.

30
Q

the __________ allows to turn any string into a function. For example, we can receive a new function from a server and then execute it:

A

new Function

31
Q

a function remembers where it was born in the special property __________

A

[[Environment]].

32
Q

when a function is created using new Function, its [[Environment]] references not the current________ Environment, but instead the________

A

Lexical

global one.

33
Q

___________ allows to run a function once after the interval of time.

A

setTimeout

34
Q

_________ allows to run a function regularly with the interval between the runs.

A

setInterval

35
Q

We may decide to execute a function not right now, but at a certain time later. That’s called _________

A

“scheduling a call”.

36
Q

The delay before run, in milliseconds (_______ ms = 1 second), by default 0.

A

1000

37
Q

the code runs after how much time?

function sayHi() {
  alert('Hello');
}

setTimeout(sayHi, 1000);

A

1 second

1000ms = 1 second

38
Q

setTimeout(“alert(‘Hello’)”, 5000);

what happens?

A

alert “hello” after 5 seconds

39
Q

pass setTimeout with sayHi, 1000

A

setTimeout(sayHi, 1000);

dont use () next to sayHi

40
Q

A call to setTimeout returns a “timer identifier”__________ that we can use to cancel the execution

A

timerId

41
Q

The setInterval method has the same syntax as setTimeout:

TRUE / FALSE

A

TRUE

42
Q

All arguments have the same meaning. But unlike _________ it runs the function not only once, but regularly after the given interval of time.

A

setTimeout

43
Q

To stop further calls, we should call _____________

A

clearInterval(timerId).

44
Q

Time goes on while alert is shown

TRUE / FALSE

A

true

45
Q

There are two ways of running something regularly.

A
  1. setInterval

2. recursive setTimeout

46
Q

What guarantees a delay between the executions?

A

Recursive setTimeout guarantees a delay between the executions, setInterval – does not.

47
Q

The recursive________ guarantees the fixed delay

A

setTimeout

48
Q

setTimeout(expression, timeout);

A

runs the code/function once after the timeout.

49
Q

runs the code/function once after the timeout.

A

setTimeout(expression, timeout);

50
Q

setInterval(expression, timeout);

A

runs the code/function in intervals, with the length of the timeout between them.

51
Q

runs the code/function in intervals, with the length of the timeout between them.

A

setInterval(expression, timeout);

52
Q

__________(function () {
something();
}, 1000); // Execute something() 1 second later.

A

setTimeout

53
Q

___________function () {
somethingElse();
}, 2000); // Execute somethingElse() every 2 seconds.

A

setInterval

54
Q

_________ should not be nested into its callback function by the script author to make it loop, since it loops by default. It will keep firing at the interval unless you call clearInterval().

A

setInterval

55
Q

if you want to loop code for animations or clocks Then use _________ setInterval or setTimeout

A

setInterval.