The stack allows you to relate the history of changes in the values of variables to the data. A description of the data and algorithms underlies programming. The stack is the basis for transferring control between functions, organizing recursion and parameter references. JavaScript syntax and semantics using the push () and pop () array methods makes it possible to manipulate the meaning and take into account the time factor. The peculiarity of the browser language and its logic allow a different look at the possibilities of the time factor.
An array of data and the logic of its formation
Just describe a variable. It’s easy to create an array of variables. An algorithm using data is a static and simple solution to a problem. Classical work with data:
- describe (create) a variable;
- Assign a value
- change value;
- delete the variable.
The push () and pop () functions allow you to change the nature of variables and their use. The idea of the stack has not changed since its birth, but JS's specificity as a browser language and modern programming concept allows us to take into account the time factor and give the data dynamics.
JS function array.push ('var value') - add something to the end of the array. JS function array.pop () - extract the last element of the array. The pointer in the context of push / pop moves when it is added to the added element, when extracted, it moves to the penultimate element, and the last element is the result of the operation.
A stack of plates - a traditional stack description in JavaScript - takes on a new meaning. Let always a variable be an array. An array itself is a collection of variables, but considering a variable as an array, you can look differently at the dynamics of its values.
Value Movement
The essence of the stack - came last, left first. You cannot extract a value outside this order. Strictly observing this rule, considering the values of the entire array as one variable, you can get the dynamics of the values of this variable in time.
In this example, adding JS array.push (...) values is one sequence of actions, extracting JS array pop () values is another sequence. Both options are interrelated. This means that the active element changes its value not only in time, but also in the logic of its change.
Recursion and dynamics of value
If a function can call itself and safely fulfill its purpose, this is full functionality. A simple example is a table. There may be other tables in the table. And each table is a row, column and cell. Each cell may contain a table. Several cells per row or column can be combined into one cell, which can contain a table. A table in a cell can have a cell with two or more tables.
It is almost impossible to implement what was said in the classical programming style, but in the recursive one it is elementary. If the functionality of the algorithm for working with the table allows you to implement yourself inside any cell, then this is JS array push. In JavaScript, this “trick” has a special meaning. Tables are a custom application. The page tree (DOM) is the work on the page.
Handlers hang on DOM elements (page tags). One option is when such a handler fires once, a completely different option is when it can call itself many times. In the context of all the handlers of all page elements, the page dynamics in time is obtained.
JS array push / pop and recursion are a slightly different idea of the logic of the page: everything changes as it is required in the current situation, and is not programmed in advance in the form of sequential refinement of the visitor's actions.