53. JavaScript Closures

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

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.

What do global variable mean?

Global variables can be used (and changed) by all scripts in the page (and in the window).

How long does global variables live?

Global variables live as long as your application (your window / your web page) lives.

miért kell a closure?

ha global szinten definiáljuk a counter változót akkor az elérhető global szinten és első szinten lévő functionokban de nem érhető el nested functionban.

What can a function access inside itself?

A function can access all variables defined inside the function, like this:

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.

Where do global variables belong to in a webpage?

In a web page, global variables belong to the window object.

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.

What is the problem here? var counter = 0; function add() { counter += 1; } add(); add(); add();

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.

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.

What is a in the last example?

a is a global variable.

Mi a megoldás?

az lenne a megoldás ha különválasztanánk a declarálást a müvelettől és csak a muveletet mentenénk el az add változóba.

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 the nested function and therefore is is closed from the gobal word totally.

example of that

function myFunction() { var a = 4; return a * a; }

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 global variable only the nested function will run and not the parent function which probably contains var variable = 0; and which would always set the whole process to zero if it ran.

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 lower. it is not available in the global environment.

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.

What is a closure?

it is a function. it is a returned nested function

What is a closure?

it is like creating a page inside the (global) page to put some variables there ONLY for a certain function to be able to use it and for other function not to be able to use it.

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.

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 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.

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.

How long does a global variable live

till the end of the page.

example of that

var a = 4; function myFunction() { return a * a; }

megoldás

var add = (function () { var counter = 0; return function () {return counter += 1;} })(); add(); add(); add(); // the counter is now 3

example of that

var add = (function () { var counter = 0; return function () {return counter += 1;} })(); add();

example of where a function can not be run.

var alma = "alma vagyok"; var kacs = "legbelul vagyok"; function korte(){ alert(alma); function nested(){ alert(kacs); } } nested(); // this can not be run here korte();

an example of where a function can be run

var alma = "alma vagyok"; var kacs = "legbelul vagyok"; function korte(){ alert(alma); function nested(){ alert(kacs); } nested(); // this can be run here } korte();

example of that.

var counter = 0; function add() { counter += 1; } add(); add(); add();

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.

we

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.

how do we prevent a normal function from running at the assigment?

we omit () after the name of the function.

how do we make a function expression run when it is assigned?

we use () after the function and we use () around the function too.

how do we run a normal function at the assignment?

we use () after the name of the function.

how?

we use nested function to use a variable but we declare that variable in the parent scope. at the first level.

when is the function itself saved into a variable in an assignment?

when the function does not run at the assignment

When is the returned value of function saved into a variable in an assignment?

when the function runs at the assignment

can a function in js have private variable?

yes it can.

Can a function access variables defined outside the function?

yes, it can. a function can also access variables defined outside the function, like this:


Kaugnay na mga set ng pag-aaral

Structures, Locations, and Functions of the Three Types of Muscular Tissue

View Set

Nutrition chapter 6, 7, 14, 15, 8, 9

View Set

Pennys Book Chapter 2 Review Questions- The Liver

View Set

Ionic Bonds, Polar Covalent Bonds, Non-polar Covalent Bonds

View Set

Chapter 2 - Database Requirements and ER Modeling

View Set