JS arrays. JS object. Multidimensional JavaScript Arrays

The transition from classical programming to object-oriented has long been proclaimed, but has not yet been completed, and all modern programming languages ​​have long had their own object-oriented concepts and syntax.

On Differences, Stack and Recursion

The differences between implementations of the object-oriented doctrine of programming languages, the difference in their philosophy and understanding of the new logic of their authors and developers is great.

dynamic array

The level of stack organization of data and the level of recursion of modern algorithms in the field of Internet programming are low and not often encountered. Local programming applies stack and recursion only when explicitly needed or when directly specifying a development task.

These circumstances suggest that the dynamics of a variable and the dynamics of a set of values ​​(an array) in the process of executing an algorithm are at the “initial” level of development of data and algorithms.

Perhaps this is not the case, and the world of Internet programming is rapidly developing.

If a variable (including in the status of an array or an object) is a stack, then a history of values ​​appears and the ability to navigate through it in search of the necessary information (value) or optimal solution (set of values).

If the function (algorithm) is a sequence of actions without recursion (without the ability to call itself) - this is a dead end that cannot develop without outside help (without the participation of the programmer).

Even if the purpose of a function is not the development of its functionality, but the function is recursive, it can claim to be a fully functional solution, and this is important, this is the level of programming and the assessment of the qualifications of the programmer.

Variable, its value and place in the algorithm

The distance between the variable and the array is negligible on the line of programming development.

Variables instantly became familiar and immediately led to the creation of various data structures. Arrays, structures, records and other constructions of the first programming languages ​​appeared.

When FORTRAN and BASIC ruled, the objects were not even dreamed of,and the classic syntax of the operator - it was a separate line of code. Only in exceptional cases, you could write a couple of statements in one line.

Fortran & Basic

Arrays appeared quickly, but so far there is no understanding of the variable as a place in the algorithm, that is, the moment of changing its meaning and concept of the history of these moments.

The concept of "rollback" - the prerogative of application programs. Operating systems and programming languages ​​never took into account the expediency and importance of the history of actions (events) and the right of the programmer / user to cancel them.

Until now, the concept of the history of actions and the movement along it (undo and redo) is not recognized by the “leading” authorities of the leading edge of information technology, but how good and right is it?

The right to cancel the decision, change behavior, search for the best decision taken:

It's time to understand and recognize this to manufacturers of operating systems and programming tools.

A variable or array is not one value or one set of values, it is always a history of values ​​and its dynamics even if this objective fact is not consciously used by the programmer.If it were otherwise, both the code and the history of changes in the values ​​of variables, arrays and objects would be debugged - this is a completely different level of reliability of the programmer’s work. This is a debugger that works at the level of the dynamics of the active functional, and not a specific position in the static code.

Browser language, its arrays and objects

Programming theory is not as popular as the practice of programming in JavaScript. This is the usual situation. A page in the browser instantly signs on a DOM tree and becomes a data source in the form:

  • variables;
  • arrays;
  • objects.

When composing a script, the programmer uses the available JS arrays, creates his own arrays, describes temporary variables, and develops efficient interface, data, and dialog objects with the visitor.

DOM: objects and arrays

There is no place for the history of values ​​and solutions, there is no time for designing change histories of variables and arrays, and for objects this is done as needed.

Result: a web resource in rare cases provides a “rollback”, and the concept of history is “long time ago” in the browser menu in the context of movement on the page history: forward, backward, or a choice from the list.

The concept of the history of values ​​at the level of variables is not claimed. In a stack and a recursion - there is no need, unless it clearly follows from the essence of the task or the projected algorithm.

Hi, variable when you're an array

For a long time programming training began with the description of the simple variable “i am variable!” And the derivation of its value “Hello, World !!”.

Variable, array and object

In this example, nothing prevents JS arrays from being turned into a variable with one value, and i_am_VARIABLE to be made an array.

There is a significant difference between “[...]” and “{...}”, but there is no difference between aSimple and oSimple. Any variable can change its type anywhere in the algorithm and at any time.

The HTML elements of the page loaded into the browser are full of events, most of which the developer monitors and assigns the necessary functionality to them in the JavaScript code. JS functions triggered by events on DOM elements can change JS arrays at any time. It gives a chance to form a story, but few people use it.

Features of descriptions of arrays and objects

The implementation of object-oriented programming (OOP) in JavaScript is the best, on the simple basis that this browser language always works on real objects.Despite the fact that real objects are objects of a page converted to a DOM, these objects are controlled by page functionality and visitor actions. And this is the experience of JS and work with arrays, as with real data.

In the above example, the filling of the variable aSimple (in fact, this is an array) occurs only with three operators, and a set of three values ​​is obtained: Fortran, Algol, and Basic. JS, as associative arrays, does not interpret the description of the variable by square brackets.

On the contrary, any attempt to use a variable otherwise than described by curly brackets is doomed to failure. In the description of objects, the quotes and encoding used are also important. If in perspective the array or object will be used in JSON format, UTF-8 encoding and only double quotes will be required.

If the description of “{...}” is applied to the variable, then this is a dynamic array with text keys, possibly a full-fledged object with methods and its own data structure.

Stack inside array

An array is a set of values. The number is not technically limited, but it is not advisable to use large amounts of data. The array element may be another array.The array itself is a simple form of the object. Creating multidimensional arrays is permissible, but a large number of dimensions may complicate the solution of the problem.

The volume of the array and the number of dimensions in it should be within reasonable limits. This simplifies the development of the algorithm and its development.

JavaScript does not impose a stack on the programmer and does not require mandatory recursiveness from the programmer’s functions. The world of tasks and solutions is too complicated to impose anything with the syntax of the language, but the tools for building the stack in JS are complete.

Stack inside array

The classical methods push and pop can be applied to any variable described by “[...]”, and this will be a dynamic array. The number of elements in it is increased by the push method, and when an element is extracted by the pop method, the last element becomes “active”.

The result of the pop method is the last element of the array; therefore, when pop is applied, the next previous element of the array will be the next. When adding an element by the push method, it comes next to the last one.

By manipulating push and pop calls, the programmer gets the stack. If arrays of JS come / go on the stack, this is the dynamics (history) of a set of values ​​or solutions.

Reverse stack of plates: an array into a string

The stack has always been compared with a stack of plates: each new plate lies only on top and can only be taken from above. Only the top plate (pop method) or the space above it (push method) is always available.

JavaScript expanded this paradigm and made it possible to work with a stack of plates both in the classic version and in the opposite: the programmer can view the beginning of the stack of plates as a stack through the first element. The shift and unshift methods are similar to pop and push, but work with the first element.

An array is a set of values, and the join and split methods are the association of an array with a string. The line between the array and the variable is missing. The following example shows this.

Join and split methods

Initially, an aSimple array of the type “[...]” was described, and it had real three values. Then aSimple turned into a variable - a string of characters. Then, three elements were written to aSimple, which turned into a single line using the join method, and created a new aStr array using the split method, without using the description “[...]”.

Real stack from DOM tree

A stack is not always an array that is arranged in a familiar way. Objects are not always methods that provide access to the contents of an object, but elements of the DOM tree always serve as the source of events that handle a set of JavaScript functions.

If we present all potential sources of an event in one time slice at the time of the occurrence of at least one event, then:

  • only one event can occur;
  • two or more events may occur;
  • it is possible to visit the same page by other or several visitors;
  • the server can be accessed by the client, that is, by the browser;
  • allowable activity AJAX process;
  • perhaps one or more timers are running.

This is the real event stack of a serious site. Implementing it as arrays or objects is almost impossible. This is a completely different level of programming, in which static code, the bodies of functions and methods are essential, but it is impossible to debug or track any process if there is no clear idea about the state and dynamics of the whole picture.

The problem of OOP and the advantage of classical programming consists in the fact that real object-oriented programming recursively launches a mass of objects that are interconnected with each other and can launch instances of objects that are already running (started by some other object or themselves).

Real PHPWord Object System

It is impossible to parse which instance functions by the code of a function or method. A code is a static, and which instance is active is a dynamic. The active point in space is a static code, but the level of recursion and the state of the actual data is a real problem.

Real Object Oriented Project

Practical and demanded JS object is obtained for managing a PHPOffice / PHPWord project. This library contains many PHP server-side objects. Most objects implement the properties of MS Word word processor objects and are designed to read and form * .docx files.

PHPOffice / PHPWord - JS object

The library does not include a JS object for managing the process of generating a result file, but includes a unique example of an interconnected system of objects.

Any paragraph of the * .docx file may contain several fonts, styles, colors, subscripts and superscripts. Modern text design uses a variety of techniques and allows you to create unique content and design documents.

Any paragraph of the * .docx file can be a table. A table can have many columns and rows, but creating multidimensional arrays in rows or columns will fail. A unique object is a cell, which can be any number in a row or column.

If you attach to a cell, then it can contain any number of paragraphs. In each paragraph there can be any formatting that is permissible when formatting each paragraph of the main text.

Any paragraph in a cell can be a table, with all the possibilities indicated above! The PHPOffice / PHPWord library is a unique example of recursion and stack organization of data. Perhaps, MS Word developers use their specific programming practices, but in JS: an array of objects that recursively create a table of tables, allowing you to have arbitrarily paragraphs, tables, attachments into each other - a reality that is implemented more efficiently than the PHP version.

Recursion and stack problems: object-oriented thinking

A paragraph contains sentences, phrases, words and objects. Any text part of a paragraph can be bold, italic or underlined. Anywhere in the text can be superscript or change the color of the letters.

The paragraph object must allow recursion on the options of the outline, but the paragraph cannot be nested into each other. You can nest a table in a paragraph. A table can have many cells, and in each of them there are many paragraphs and many tables.

Creating objects by text properties:

  • fat content;
  • incline;
  • underline;
  • strikethrough;
  • color change;
  • superscript;
  • subscript;
  • and etc. -

leads to formatting objects. When deleting paragraph elements, elements are not actually deleted. MS Word provides the ability to rollback, that is, restore the deleted item. The history of undo / redo in word processors is the norm of things. This cannot be said about JS: deleting an element of an array means losing it, if you do not foresee your own rollback option.

A paragraph object cannot include objects of sentences, phrases, or words. This is redundant, not modern and will allow to build a coherent system of objects * .docx- document.

A table is an independent object, but closely related to the paragraph object. These two objects can call each other mutually, and the level of nesting of such calls is not limited. This raises the problem of the level at which the programmer thinks or the debugger works.

It is very difficult to formulate the next solution or find an error, if you think in a classical way at the code level. But if you have an idea about the dynamics of the data and the current level of nesting of calls (stack), then you can easily move on, discovering in the course of development, bugs, uncertainties and weaknesses.

js work with arrays

The ideal foundation for organizing a stack is a JS string, not necessarily in JSON format, convenient for exchange between a server and a client. Transforming the server component - the PHPOffice / PHPWord library - into an AJAX request sequence, you can get a readable version of the “movement” in the actual document.

In fact, the library's functionality disassembled the document and folded the object system, but you can view it in the browser and build an adequate dynamic object system.

On JS, adding a string sequence to an array is not a problem. The protocol of recursive levels will be available, the logic of moving through nesting levels is obvious. It is possible to create a history of movements in the values ​​of variables, arrays, code of methods of objects and functions.

Recursion in the system of different objects

Objects and arrays in JavaScript are always a good solution. Projects of the PHPOffice / PHPWord class are always a good server version of the application. There is a difference between PHP programming and browser programming.

The browser object "lives" as long as the visitor is on the site. Even when the visitor changes the page of the site to another, JavaScript allows the developer to move to the new page an object already created.

The developer can independently manage the creation of pages and moving through the desired DOM trees. But by themselves, objects and arrays in JavaScript are not real objects and visitor data. In the case of documents, a visitor can be a student, a future PhD, an employee of the city administration, etc. For these categories of visitors, you need real objects: documents, not DOM elements native to JavaScript

Projects class PHPOffice / PHPWord - an excellent solution and experience in creating complementary systems of objects, the ability to track recursion, accounting for stack operations, managing the creation of various objects nested into each other.

Real system of objects

The object of JavaScript is the desire for dynamics, but when this desire is supported by the real practice of a good server application, a good real problem with a real system of objects is that the desire is objective, workable, reliable and achievable.

Related news

JS arrays. JS object. Multidimensional JavaScript Arrays image, picture, imagery


JS arrays. JS object. Multidimensional JavaScript Arrays 75


JS arrays. JS object. Multidimensional JavaScript Arrays 47


JS arrays. JS object. Multidimensional JavaScript Arrays 36


JS arrays. JS object. Multidimensional JavaScript Arrays 61


JS arrays. JS object. Multidimensional JavaScript Arrays 43


JS arrays. JS object. Multidimensional JavaScript Arrays 58


JS arrays. JS object. Multidimensional JavaScript Arrays 10


JS arrays. JS object. Multidimensional JavaScript Arrays 76


JS arrays. JS object. Multidimensional JavaScript Arrays 67