Level 125 Level 127
Level 126

[Quiz] Closures

36 words 0 ignored

Ready to learn       Ready to review

Ignore words

Check the boxes below to ignore/unignore words, then click save at the bottom. Ignored words will never appear in any learning session.

All None

What can a function access inside itself?
A function can access all variables defined inside the function, like this:
Can a function access variables defined outside the function?
yes, it can. a function can also access variables defined outside the function, like this:
a is a global variable.
What is a in the last example?
Where do global variables belong to in a webpage?
In a web page, global variables belong to the window object.
What do global variable mean?
Global variables can be used (and changed) by all scripts in the page (and in the window).
When is a variable local?
A local variable can only be used inside the function where it is defined. It is hidden from other functions and other scripting code.
Are global and local variables with the same name different variables?
Global and local variables with the same name are different variables. Modifying one, does not modify the other.
is it global or local if we create a variable inside a function without var?
Variables created without the keyword var, are always global, even if they are created inside a function.
How long does global variables live?
Global variables live as long as your application (your window / your web page) lives.
How long does local variable live?
Local variables have short lives. They are created when the function is invoked, and deleted when the function is finished.
What is counter dilemma?
Suppose you want to use a variable for counting something, and you want this counter to be available to all functions.
yes it can.
can a function in js have private variable?
we use nested function to use a variable but we declare that variable in the parent scope. at the first level.
miért kell a closure?
ha global szinten definiáljuk a counter változót akkor az elérheto global szinten és elso szinten lévo functionokban de nem érheto el nested functionban.
Mi a megoldás?
az lenne a megoldás ha különválasztanánk a declarálást a müvelettol és csak a muveletet mentenénk el az add változóba.
var add = (function () {
What is a closure?
an inner function that always has access to the vars and parameters of its outer function, even after the outer function has returned.`
why do we call it a closure?
because it was nested in another function it does not have access to the global environment. the other function in which it was nested has an access to the global environment but we call…
What does it mean that a nested function has access to its parent function?
it mean that if we declare a variable in the parent function that variable is still available in the nested function without passing it as an argument.
What is the advantage of a closure?
we can declare and define a variable in a parent function then build the code in the closure which we can use later without using the declaration of the variable.
What does it mean that we create a global variable?
it means that it can be acessed globally from every funtion even from the nested functions too.
What is closure function?
if we define a variable in a parent function and build a code in the nested function with that variable and save the nested function into a global variable then when we call the gl…
At which level can a function be run?
in js a function can only be run from the parent environment. can not be run from grand parent evironment.
var alma = "alma vagyok";
example of where a function can not be run.
What does it mean that a function has an access to the parent evironment?
if we use a variable in an operation in the nested function but the variable was created in the parent function that variable is always available in the parent function but not one level lo…
When we call a function what do we do?
it is like we put the code from the code block in the place of the call of the function. we replace the call of the function with the content of the codeblock of the same function.
till the end of the page.
How long does a global variable live
The problem is, that any script on the page can change the counter (variable), without calling add(). because counter is global variable available inside the function and the global level too.
we creat a vaiable at a parent level and it is only avaiable for the nested function but not at the global level which is one level down from a parent level.
when the function runs at the assignment
When is the returned value of function saved into a variable in an assignment?
when the function does not run at the assignment
when is the function itself saved into a variable in an assignment?
how do we make a function expression run when it is assigned?
we use () after the function and we use () around the function too.
so what happens if we assign a self-evoked anonymous function expression to a variable?
only the returned value will be saved into the variable because the function runs.
and what happens if we assign a anonymous function expression into a variable without evoking it?
the whole function will be saved but not the returned value because the function does not run.
we use () after the name of the function.
how do we run a normal function at the assignment?
we omit () after the name of the function.
how do we prevent a normal function from running at the assigment?