Ad. Javascript.info part 4 Flashcards

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

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;
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
``` let sayHi = function func(who) { if (who) { alert(`Hello, ${who}`); } else { func("Guest"); // use func to re-call itself } }; ``` func(); //
``` // But this won't work: func not defined ```
26
``` let sayHi = function func(who) { if (who) { alert(`Hello, ${who}`); } else { func("Guest"); // use func to re-call itself } }; ``` sayHi(); //
Hello, Guest
27
WHATS THE ISSUE WITH THIS CODE? ``` let sayHi = function(who) { if (who) { alert(`Hello, ${who}`); } else { sayHi("Guest"); } }; ```
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
``` let sayHi = function(who) { if (who) { alert(`Hello, ${who}`); } else { sayHi("Guest"); } }; ``` ``` let welcome = sayHi; sayHi = null; ``` welcome(); //
Error: sayHi is not a function
29
``` let sayHi = function func(who) { if (who) { alert(`Hello, ${who}`); } else { func("Guest"); } }; ``` ``` let welcome = sayHi; sayHi = null; ``` welcome(); // returns and why
welcome guest Now it works, because the name "func" is function-local.
30
the __________ allows to turn any string into a function. For example, we can receive a new function from a server and then execute it:
new Function
31
a function remembers where it was born in the special property __________
[[Environment]].
32
when a function is created using new Function, its [[Environment]] references not the current________ Environment, but instead the________
Lexical global one.
33
___________ allows to run a function once after the interval of time.
setTimeout
34
_________ allows to run a function regularly with the interval between the runs.
setInterval
35
We may decide to execute a function not right now, but at a certain time later. That’s called _________
“scheduling a call”.
36
The delay before run, in milliseconds (_______ ms = 1 second), by default 0.
1000
37
the code runs after how much time? ``` function sayHi() { alert('Hello'); } ``` setTimeout(sayHi, 1000);
1 second 1000ms = 1 second
38
setTimeout("alert('Hello')", 5000); what happens?
alert "hello" after 5 seconds
39
pass setTimeout with sayHi, 1000
setTimeout(sayHi, 1000); dont use () next to sayHi
40
A call to setTimeout returns a “timer identifier”__________ that we can use to cancel the execution
timerId
41
The setInterval method has the same syntax as setTimeout: TRUE / FALSE
TRUE
42
All arguments have the same meaning. But unlike _________ it runs the function not only once, but regularly after the given interval of time.
setTimeout
43
To stop further calls, we should call _____________
clearInterval(timerId).
44
Time goes on while alert is shown TRUE / FALSE
true
45
There are two ways of running something regularly.
1. setInterval | 2. recursive setTimeout
46
What guarantees a delay between the executions?
Recursive setTimeout guarantees a delay between the executions, setInterval – does not.
47
The recursive________ guarantees the fixed delay
setTimeout
48
setTimeout(expression, timeout);
runs the code/function once after the timeout.
49
runs the code/function once after the timeout.
setTimeout(expression, timeout);
50
setInterval(expression, timeout);
runs the code/function in intervals, with the length of the timeout between them.
51
runs the code/function in intervals, with the length of the timeout between them.
setInterval(expression, timeout);
52
__________(function () { something(); }, 1000); // Execute something() 1 second later.
setTimeout
53
___________function () { somethingElse(); }, 2000); // Execute somethingElse() every 2 seconds.
setInterval
54
_________ 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().
setInterval
55
if you want to loop code for animations or clocks Then use _________ setInterval or setTimeout
setInterval.