concept stack in category javascript

appears as: stack, A stack, The stack, stack, stacks
Functional Programming in JavaScript

This is an excerpt from Manning's book Functional Programming in JavaScript.

Adding the empty array triggers the base case, naturally returning zero. Otherwise, for non-empty arrays, you proceed to recursively extract and sum the first elements together with the rest of the array. Behind the scenes, recursive calls are stacked on top of each other. As soon as the algorithm reaches the terminating condition, all the return statements are executed as the runtime unwinds the stack to let the addition take place. This is the mechanism by which recursion cedes looping to the language runtime. Here’s a step-by-step view of the sum algorithm you just implemented:

A stack is a basic data structure that contains objects such that insertion and removal follow a last-in first-out (LIFO) approach. Consider the analogy of a pile of dishes stacked one on top of another: all operations on the stack are performed at the top.

The context stack is a component of the JavaScript programming model responsible for managing the execution of a function and the variables it closes over (if you don’t know what this means, please revisit closures in section 2.4). The stack always starts with the global execution context frame, which contains all global data, as shown in figure 7.1.

Figure 7.1. JavaScript’s execution context stack on initialization. Depending on how many scripts are loaded on the page, the global context can keep track of lots of variables and functions.

The global context frame always resides at the bottom of the stack. Each function context frame takes up a certain amount of memory depending on the number of local variables contained within it. Without any local variables, an empty frame is approximately 48 bytes. Local variables and parameters like numbers and Booleans require 8 bytes each. Intuitively, the more variables the function body declares, the larger the stack frame. Each frame contains roughly the following information:[1]

Figure 7.2. When invoking any function, like logger in this case, the single-threaded JavaScript runtime pauses the current global context and activates the context for the new function to run. At this point, a link is created between the global context and the function context, traversable via the scopeChain. Once logger returns, its execution context is popped off the stack, and the global context resumes.

Internally, the logger function makes calls to other Log4js operations, which create new function contexts that are put on the stack (if you haven’t done so, you can visit the appendix for an introduction to Log4js). Due to closures in JavaScript, the function contexts resulting from inner function calls are stacked one on top of the other, each taking up its fair share of allocated memory and linked via the scopeChain reference (see figure 7.3).

Figure 7.3. How the function context grows when running nested functions. Because each function produces a new stack frame, the stack grows in proportion to the level of nesting in functions. Both currying and recursion rely on nested function calls.

Finally, once the Log4js code completes, it gets popped off the stack; the logger function follows suit, leaving the runtime back in its original state—with only the single global context running (refer back to figure 7.1). This is the magic behind closures in JavaScript.

Prototype and Scriptaculous in Action

This is an excerpt from Manning's book Prototype and Scriptaculous in Action.

Look at the order in which we specified the effects, and where we placed them according to the values of the queue option. First, we added the MoveBy effect to the front of the queue. Then we added the Scale effect to the front of the queue, which placed it before the MoveBy effect. Finally we placed the Opacity effect, again at the front of the queue, which placed it before the Scale effect. The result is that they execute in the reverse order that the effects were added. By placing the effects at the front of the queue, we effectively created a stack of the effects.

10.2.2. Treating Arrays like stacks : pop() and push(), shift() and unshift()

In addition to behaving like ordinary arrays, JavaScript Arrays act like stacks. A stack is a collection of items in which elements can only be added to or removed from the end. In many card games, the deck of cards is treated as a stack. Snippet 10.3 shows how JavaScript Arrays exhibit stack-like behavior.