ComputersProgramming

JS foreach object as variants design

The syntax of JavaScript provides the ability to combine variables into arrays and objects. Access to elements can be obtained through indexes and names, by means of loop operators.

But it is not always possible to know the number of elements in the collection at the design stage and it is not always convenient to use the syntax of the language within the semantics that it provides.

Classic Designs

The usual loop statement "for (var i = 0; i

Var i = 0;
While (typeof aData [i]! = 'Undefined') {... aData [i]; ... i ++; },

When the number of elements is unknown.

You can also build other algorithms for filling and processing arrays, but it is much more convenient to use new, modern options.

The classics are good when you need to process the personnel department questionnaires (surname, name, patronymic, post), equipment cards (model, manufacturer, seller, sales date) and other data that do not change structurally and are of aggregate interest.

When an element of an array is itself an object or structure, has its own properties and methods, then the classics can not cope with the dynamics of quantity and quality, and the array takes on a different meaning.

Cycles for each element

In dynamics, the manifestation of an array of properties of the actual element is essential. The JS foreach array construct, which is somewhat different from the usual (accepted in other languages), allows the array to show its properties through its current element.

For example, one array contains roads (their qualitative and quantitative characteristics), and another array is cars that can only travel on certain roads and have different volumes of fuel tanks, that is, the distances between gas stations also have significance.

In this version, an expedient algorithm should take as a basis, for example, the road and choose cars that can pass through it. And it is better, if the road and the car, acting in the program as objects, "find themselves". This is a natural application of object-oriented programming, which, among other things, does not lead to the need to change the algorithm when changing collections of roads and cars, automatically takes into account roads in repair, cars on maintenance, etc.

Technically, on JS foreach, the structure looks very simple: "aData.forEach (fData);", where fData is the function that is applied to each element of the array aData:

Function fData (value, index) {sText + = index + '=' + value + '; '; }.

If the original data is presented as follows:

Var aData = [1,2,3,4];
AData.push (11);
AData.push (22);
AData.push (44);
AData.push ('line 1');
AData.push ('line 2');

Var sText = '',

Then the result of this JS foreach application will be:

"0 = 1; 1 = 2; 2 = 3; 3 = 4; 4 = 11; 5 = 22; 6 = 44; 7 = line 1; 8 = line 2;".

Features of arrays with objects

The object in JavaScript is special. The use of objects in this language differs significantly from implementations in other languages. An object is an array of properties and methods. In this case, the latter actually perform the actions, preserving or editing their own or external content of other objects, arrays, variables.

Objects coming to the array as frames on a film, when looking at JS foreach object constructs, form a new semantics: the construction of a variable meaning .

So, the roads themselves are given the opportunity to choose cars that can travel through them, and the latter filter the days available for them routes. If you do not pay attention to that redundancy in this programming option raises the reliability of the code, the task - to determine the route of delivery of the cargo - turns the JS foreach design into a meaningful and simple solution: to spread the routes along the roads, select the machines and perform the delivery of the goods.

If the routes, roads and machines are arrays of objects, then the algorithm is much simpler. It would seem strange that the appearance of the JS foreach design took so long to wait. However, despite the simplicity of the written, in practice it is rather difficult to implement it.

Practice JS foreach and real objects

The thinking of the modern developer operates with the usual objects for programming, but not the objects of the real problem. It is not customary to understand as such a road, car, route, distance ...

Object-oriented programming developed in thorny ways, and traditionally it became customary to create program objects: an array, a form, a button, a route selection window, and so on.

In addition, JS foreach along with other language constructs is executed in the visitor's browser, which introduces serious limitations in practice. Not every developer is ready to share his code. Effective examples of the use of arrays and collections of real objects are a good way to improve knowledge.

However, the availability of JavaScript code for the developer's unintended use has an indirect effect on the development of promising ideas for creating dynamic and real objects. The JS foreach constructs are not yet perfect, and the development of their semantics seems to be a very promising direction.

Similar articles

 

 

 

 

Trending Now

 

 

 

 

Newest

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