Professor: Oh, sorry, I forgot to give the formal definition. A closure is a function
together with scope from which it can use variables. If you want, you can also check
which variables are accessible insid e the current closure in the DevTools:
You should place a breakpoint inside the anonymous function and reload the page
to see the situation like on this screenshot. Notice how the variable i is no t a local
variable of the anonymous function but is accessible inside its c losure as yo u can see at
the right side of the DevTools window. Incid entally, if you want i to get incr emented
to two, then you must step over several function calls by pressing F10 several times.
However, if there was no brea kpoint inside the function, then you wouldn’t be able to
enter the function using the
Step Over command. To enter a function, you c an use the
Step Into debugger command or press F11.
10.7 Homework
Professor: For homework I want you to write and use a f unction that will help you
to generate a math worksheet for kids to practice additio n, subtraction, multiplication,
and division. The function should a ccept two arguments: the desired operation and the
maximum allowed value for the two operands. Let the function generate and return
a r andom equation in the form of a string togeth er with th e corre sponding solution.
You can pack both in a two-element array. Additionally, take care that the result of a
subtraction is never negative and that the result of a division is always an integer with
no re mainder.
The last thing for today is, of course, a list of keyword s:
In this meeting: function, call/invoke, argument, parameter, f unction definition
statement, function expression, return, function body, function nam e, hoisting,
optional parameters, arguments object, reference to fu nction, passing arguments
by refer ence, passing fun ction as argument, so rt(), variable scope, local variable,
function scope, global variable, scope chain, closure
204 Meeting 10. Understa nding Functions
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.119.132.223