ComputersOS

The JavaScript stack push / pop

JavaScript does not have a stack, but there is an array that can be used as a stack. In this case, manipulating the methods of the JavaScript array push / pop, you can have at your disposal a stack, an array and your own data organization.

In the first approximation, arrays are a familiar and popular data structure. But working with them as a stack gives them opportunities not provided by the syntax of the language. Adding / removing via push / pop to the end or unshift / shift to the beginning is not only convenient, but also practical.

Use of methods

An array can be refilled with new elements using the push method. The result of this method is a new number of array elements. The reverse procedure - the pop method has no parameters, but produces the last element of the array as a result.

As follows from the syntax and logic of the language, arrays can work with any type of data.

JavaScript push object - nonsense or progress?

The language of the browser is not inferior to its more "free" colleagues with respect to object-oriented programming, that is, it also makes it possible to create objects. At the same time, the keyword that denotes something related to the PLO does not.

Generally speaking, what is in JavaScript, has not yet allowed itself to have any "free" of the browser programming language. The most original - the creation of an object here - is the work of the programmer, starting with the name of the object.

JavaScript methods pop & push when using objects provide the programmer the opportunity to create a multifunctional object in the direct meaning of the word.

For example, having several interrelated, but different pages (objects, not connected in any way with the logic of the dialogue), it is possible to realize movement on them by the visitor. Putting an initial page object on the stack (array) by push method (the visitor came), give him a choice of further actions.

The next push will place the page object the visitor has selected on top. Rollback pop will return it back. Moving on is another push, and this will form the dialogue of the current visitor. This can be useful to the developer in terms of experience and statistics, and to provide navigation in the current session of the site.

Stack, array and organization of data

There are many problems when the result requires a multivariate choice. If you select the set of if or case statements for the implementation, you get a large, long and branchy "bush" of conditions.

In general, this is not the worst decision, but when something needs to be changed, it will be necessary to remember for a long time what condition is following what, and the algorithm will turn out to be illegible, and the most unpleasant can become a source of hard-to-find errors.

With the help of a stack, in almost all cases, you can do easier.

There is a task: you need to select an artist from a hundred available. Each performer can do something from three positions (one to three in any combination):

  • T - do maintenance;
  • S - can completely perform repair work;
  • I - has the right to do warranty repairs.

In order to quickly select the contractor for the order with the desired type (works), you can do three JavaScript push operations and merge the array into one line.

Search by line in the line is always clearer than multiple conditions. This simple case is only three to three variants, but even here there will be much more code than in one comparison of only two lines.

Similar articles

 

 

 

 

Trending Now

 

 

 

 

Newest

Copyright © 2018 en.atomiyme.com. Theme powered by WordPress.