concept track in category javascript

This is an excerpt from Manning's book Get Programming with JavaScript Next: New features of ECMAScript 2015, 2016, and beyond.
Because this book covers a mixture of ES2015, ES2016, and proposed/staged features, I’ll define some terms to make keeping track of all this easier. Going forward, I may refer to ES2015 interchangeably with ES6, and when I do, I’m always referring to the same thing: the Sixth Edition to ECMAScript. Likewise, I may refer to ES7 and ES2016 interchangeably. I may use the term ESNext as a blanket term to refer to ES2015 and later—basically everything that’s new to JavaScript after ES5.
The game is going to render cards. Each card can be displayed face down or face up. The cards are going to be passed around from the deck to each player, so keeping track of which ones are flipped could be tricky. This becomes easy with a Map, though. You can store the actual card as the key in your Map, and whether or not it’s flipped as the value. This means as long as you have a reference to the card, you can determine if it’s flipped face up or face down, as shown in the following listing.
In this capstone, you built a blackjack game by breaking your logic into cohesive modules. You started with elements and template modules. You then created a cards module followed by a utils module. Your cards module represented a deck of cards as well as hands of cards using Sets. You also used a Map to keep track of whether specific cards were face up or face down. Once you had your game logic separated into logical modules, tying the pieces together to make your game in the root index file was nice and easy.
You can take this game further by adding a Play Again button and keep track of the odds that the player beats the dealer.

This is an excerpt from Manning's book Secrets of the JavaScript Ninja, Second Edition.
Whenever an event occurs, the browser should execute the associated event-handler function. But there’s no guarantee that we have extremely patient users who will always wait an appropriate amount of time before triggering another event. For this reason, the browser needs a way to keep track of the events that have occurred but have yet to be processed. To do this, the browser uses an event queue, as shown in figure 2.8.
Listing 5.4. Using a closure in a timer interval callback
<div id="box1">First Box</div> #A <script> function animateIt(elementId) { var elem = document.getElementById(elementId); #B var tick = 0; #C var timer = setInterval(function(){ #D if (tick < 100) { #E elem.style.left = elem.style.top = tick + "px"; #E tick++; #E } #E else { clearInterval(timer); assert(tick === 100, #F "Tick accessed via a closure."); assert(elem, "Element also accessed via a closure."); assert(timer, "Timer reference also obtained via a closure." ); } }, 10); #G } animateIt("box1"); #H </script> #A - Creates the element that we’re going to animate #B - Inside the animateIt function, we get a reference to that element. #C - Establishes a counter to keep track of animation ticks (steps) #D - A built-in function that creates and starts an interval timer, given a callback #E - The timer callback is invoked every 10 milliseconds. For 100 ticks, it adjusts the position of the element. #F - After 100 ticks, we stop the timer and perform tests to assert that we can see all relevant variables needed to perform the animation. #G - The setInterval duration—the callback will be called every 10ms. #H - Now that it’s all set up, we set it in motion! !@%STYLE%@! {"css":"{\"css\": \"font-weight: bold;\"}","target":"[[{\"line\":2,\"ch\":21},{\"line\":2,\"ch\":25}],[{\"line\":3,\"ch\":8},{\"line\":3,\"ch\":12}],[{\"line\":7,\"ch\":8},{\"line\":7,\"ch\":12}],[{\"line\":16,\"ch\":15},{\"line\":16,\"ch\":19}],[{\"line\":26,\"ch\":17},{\"line\":26,\"ch\":21}],[{\"line\":27,\"ch\":63},{\"line\":27,\"ch\":67}],[{\"line\":30,\"ch\":104},{\"line\":30,\"ch\":108}],[{\"line\":4,\"ch\":8},{\"line\":4,\"ch\":12}],[{\"line\":6,\"ch\":10},{\"line\":6,\"ch\":14}],[{\"line\":7,\"ch\":43},{\"line\":7,\"ch\":47}],[{\"line\":8,\"ch\":8},{\"line\":8,\"ch\":12}],[{\"line\":14,\"ch\":15},{\"line\":14,\"ch\":19}],[{\"line\":28,\"ch\":54},{\"line\":28,\"ch\":58}],[{\"line\":30,\"ch\":66},{\"line\":30,\"ch\":70}],[{\"line\":31,\"ch\":15},{\"line\":31,\"ch\":19}],[{\"line\":5,\"ch\":8},{\"line\":5,\"ch\":13}],[{\"line\":11,\"ch\":22},{\"line\":11,\"ch\":27}],[{\"line\":18,\"ch\":15},{\"line\":18,\"ch\":20}],[{\"line\":29,\"ch\":61},{\"line\":29,\"ch\":66}],[{\"line\":30,\"ch\":9},{\"line\":30,\"ch\":14}],[{\"line\":31,\"ch\":34},{\"line\":31,\"ch\":39}]]"} !@%STYLE%@!
Figure 5.19. By creating multiple closures, we can do many things at once. Every time an interval expires, the callback function reactivates the environment that was active at the time of callback creation. The closure of each callback automatically keeps track of its own set of variables.
![]()
Every time we call the animateIt function, a new function lexical environment is created
![]()
that keeps track of the set of variables important for that animation (the elementId; elem, the element that’s being animated; tick, the current number of ticks; and timer, the ID of the timer doing the animation). That environment will be kept alive as long as there’s at least one function that works with its variables through closures. In this case, the browser will keep alive the setInterval callback until we call the clearInterval function. Later, when an interval expires, the browser calls the matching callback—and with it, through closures, come the variables defined when the callback was created. This enables us to avoid the trouble of manually mapping the callback and the active variables
![]()
![]()
, thereby significantly simplifying our code.
that keeps track of the set of variables important for that animation (the elementId; elem, the element that’s being animated; tick, the current number of ticks; and timer, the ID of the timer doing the animation). That environment will be kept alive as long as there’s at least one function that works with its variables through closures. In this case, the browser will keep alive the setInterval callback until we call the clearInterval function. Later, when an interval expires, the browser calls the matching callback—and with it, through closures, come the variables defined when the callback was created. This enables us to avoid the trouble of manually mapping the callback and the active variables
, thereby significantly simplifying our code.