Ad. Javascript.info part 4 Flashcards
Advanced working with functions Global object Function object, NFE The "new Function" syntax Scheduling: setTimeout and setInterval
In a browser it is named _______for Node.js it is ______, for other environments it may have another name.
“window”,
“global”
alert(“Hello”);
// the same as \_\_\_\_\_\_\_\_\_alert("Hello");
window.
Open Google using alert
window.open(‘http://google.com’);
shows the browser window height with alert
alert(window.innerHeight); //
Top-level_______ variables and _________automatically become properties of window.
var
Function decelerations
var x = 5;
alert(window.x); //
window.x = 0;
alert(x); //
5 (var x becomes a property of window)
0, variable modified
let x = 5;
alert(window.x);
undefined
the value of “this” in the global scope is ______.
window
As of now, the multi-purpose window is considered a design mistake in the language to avoid this Javascript has a built in ______
modules.
fix script below so x becomes “undefined”.
var x = 5;
alert(window.x);
let x = 5;
alert(window.x); // undefined
let x = 5;
alert(x); //
5
alert(this); //
undefined
Using global variables is generally discouraged.
TRUE / FALSE
TRUE
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 _______
window
global
As we already know, functions in JavaScript are_____.
Every value in JavaScript has a type. What type is a function?
In JavaScript, functions are _________
values
objects
function sayHi() { alert("Hi"); }
alert(sayHi.name); //
sayHi
function sayHi() { alert("Hi"); }
alert(__________); // sayHi
sayHi.name
WRITE AS A FUNCTION DECLEARATION
let sayHi = function() { alert("Hi"); }
function sayHi() { alert("Hi"); }
function f2(a, b) {}
alert(f2.length); //
2
function many(a, b, ...more) {} alert(many.length); //
2
A property is a variable
TRUE / FALSE
FALSE
A property is not a variable
function sayHi() { alert("Hi"); sayHi.counter++; } sayHi.counter = 0;
sayHi(); // Hi
sayHi(); // Hi
console.log(sayHi.counter);
2
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;
IS NOT
What are the two special things about the name function?
- It allows the function to reference itself internally.
2. It is not visible outside of the function.
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
let sayHi = function func(who) { if (who) { alert(`Hello, ${who}`); } else { func("Guest"); // use func to re-call itself } };
sayHi(); //
Hello, Guest
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.
let sayHi = function(who) { if (who) { alert(`Hello, ${who}`); } else { sayHi("Guest"); } };
let welcome = sayHi; sayHi = null;
welcome(); //
Error: sayHi is not a function
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.
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
a function remembers where it was born in the special property __________
[[Environment]].
when a function is created using new Function, its [[Environment]] references not the current________ Environment, but instead the________
Lexical
global one.
___________ allows to run a function once after the interval of time.
setTimeout
_________ allows to run a function regularly with the interval between the runs.
setInterval
We may decide to execute a function not right now, but at a certain time later. That’s called _________
“scheduling a call”.
The delay before run, in milliseconds (_______ ms = 1 second), by default 0.
1000
the code runs after how much time?
function sayHi() { alert('Hello'); }
setTimeout(sayHi, 1000);
1 second
1000ms = 1 second
setTimeout(“alert(‘Hello’)”, 5000);
what happens?
alert “hello” after 5 seconds
pass setTimeout with sayHi, 1000
setTimeout(sayHi, 1000);
dont use () next to sayHi
A call to setTimeout returns a “timer identifier”__________ that we can use to cancel the execution
timerId
The setInterval method has the same syntax as setTimeout:
TRUE / FALSE
TRUE
All arguments have the same meaning. But unlike _________ it runs the function not only once, but regularly after the given interval of time.
setTimeout
To stop further calls, we should call _____________
clearInterval(timerId).
Time goes on while alert is shown
TRUE / FALSE
true
There are two ways of running something regularly.
- setInterval
2. recursive setTimeout
What guarantees a delay between the executions?
Recursive setTimeout guarantees a delay between the executions, setInterval – does not.
The recursive________ guarantees the fixed delay
setTimeout
setTimeout(expression, timeout);
runs the code/function once after the timeout.
runs the code/function once after the timeout.
setTimeout(expression, timeout);
setInterval(expression, timeout);
runs the code/function in intervals, with the length of the timeout between them.
runs the code/function in intervals, with the length of the timeout between them.
setInterval(expression, timeout);
__________(function () {
something();
}, 1000); // Execute something() 1 second later.
setTimeout
___________function () {
somethingElse();
}, 2000); // Execute somethingElse() every 2 seconds.
setInterval
_________ 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
if you want to loop code for animations or clocks Then use _________ setInterval or setTimeout
setInterval.