/ / Javascript: function in function. JS programming language

Javascript: function in function. JS programming language

The ideas of dynamic content creation on the web-resource have become the norm. Static pages and template site building finally completed their mission.

However, a modern web resource does not have to be represented by a set of pages formed by the server and updated by the browser (JS + AJAX).

javascript function in function

A web resource at the time of arrival of the visitor can be a pair of headers for the protocol, some text in the “head”, several lines of code in the “body” and everything. The rest "think of“In the process of a visitor’s work, this is the perfect site or one who wants to be one.

Description place and the essence of the functions

Javascript is experience gained over many decades.It has a significant history of development, a modern, qualified team of creators and developers. The language is well thought out, reliable, beautiful and gives a real opportunity for developers to write decent code and improve themselves.

javascript error the operation is insecure

The concept of an algorithm outside the function is missing here.principle. Of course, the developer can insert a script anywhere on the page, place the code in it and it will be executed. But what is the point in the code that is executed only once: when loading (overloading) pages? Unless you can set the initial values ​​of some little significant variables.

The script is a place to describe the desired variables andfunctions, rather than a good piece of code written for its own sake. It is the set of functions that is essential and significant, perhaps - their mutual direct connection, but more often everything is different. The place of the description of the function and the place of its application are not the same.

It is not necessary that the function becall another function directly; it can be done indirectly through dynamic code generation. The visitor makes a decision within the framework of this code and a completely different system of functions is triggered.

Functional dynamics

Functional dynamics is not onlynot so much the handlers assigned to the elements of the page, these are the functions that form the elements of the page, and the immediate handlers can also change.

javascript return

The action on the page unfolds depending on its elements and the visitor's behavior on it. Mouse movements, keyboard buttons, clicks, item events, and other circumstances lead to the launch of the desired functions.

Initially there is no consistency andThere is no parallelism. There is an adequate response of a web-resource to events. How quickly JavaScript executes a particular function depends on many technical (computer, communication lines) and semantic (algorithm logic, subject area, task meaning) factors.

In fact, it can be argued that something workedin parallel, but something will be fulfilled after something, but there is no special meaning in it. It is important that the JavaScript function is the ability to create an adequate response to the visitor’s actions.

This is a new thinking in the development: distributed information processing in the depths of a single browser!

Syntax of variables and functions

JavaScript-переменные размещаются как в теге "Script", and in the body of the function. Functions are defined in the same way. There is no particular point in writing another function inside a function, but this may be necessary for various and well-founded reasons.

The function description generally starts with the keyword “function”, followed by its name, the list of arguments in parentheses separated by comma and the function body in curly brackets.

javascript pass function to function

This example describes two functions that provide AJAX exchange between the page and the server. The scXHR variable is described above, therefore it is available both in InitXML and inside WaitReplySC.

The name of the function and the parameter "function"

Здесь был представлен асинхронный вариант, когда The javascript function in the function is called after the server responds. At the same time, after receiving a response from the server, WaitReplySC refers to the tags of the page, fills them with the received information and calls other functions that may well initiate the next request to the server.

It is also important to note here that WaitReplySC is a function. But in line scXHR.onreadystatechange = WaitReplySC it is passed as a parameter.This is a general rule for transferring functions to other functions as parameters. I specified parentheses and passed its parameter (parameters) to them - the function will be executed immediately. Transferred only the name, so what with that. The function call will be made by the one who received its name.

The functionality implemented through AJAX allows you to perform JavaScript function call through data received from the server. In fact, when sending a request to the server, one or another function may not at all “know” what function it is accessing and with what information.

Exit function and its result

You can write any statements in the function body.language, which, in fact, for this purpose is intended. Variables inside and outside the function are available inside the function, but not those described in other functions.

javascript function call

If you want the function to returnresult, you can use the JavaScript return statement: return. There may be a sufficient number of return statements in the function body. It is not necessary at all that they will all return the result of the same type.

Usually, developers very much honor this opportunity and, depending on the situation, make a decision to exit the function as soon as it becomes possible.

It is not necessary to run through the entire algorithm of the function when it is possible to exit earlier.

Function arguments

Arguments to the function are passed through the listcomma, enclosed in parentheses and are immediately after its name. Variable names are used as arguments, but you can also pass values ​​directly. To pass a function to a function in JavaScript, you just need to specify its name without brackets.

javascript variables

Inside the function, the arguments variable is available, having a property length. You can refer to any function argument through arguments [0], arguments [1], ... until the last arguments [arguments.length-1].

Changing the function argument is really insidefunctions, but not outside of it. In order to change something outside the function, you need to use the JavaScript operator return, through which to transfer the necessary value to the outside.

After the function completes, all thatwas associated with its execution, will be destroyed. At run time, the function can change external variables, except for those described in other functions, including internal ones.

Arguments has a callee property thatis intended to call a function that is being executed at a given time. If you call yourself, the option JavaScript function in the function will allow recursion.

Use of functions

The main function concern is event servicing.browser. To do this, almost every tag has the ability to specify the name of the event and the function that processes it. You can specify multiple events, but only one function is specified for each event.

One function can serve multiple page elements and multiple events. Using the “this” parameter, you can transfer information to the function from where it was called.

Classic use of JS functions -event handlers on elements. In this example, the scfWecomeGo () or scfWelcomeCancel () function will be called in the visitor's login / exit form, and when you select the operating mode, scfMenuItemClick (this).

javascript return

In the latter case, the parameter is "this",which allows you to miraculously find out exactly which diva the call originated from. In general, JavaScript is so qualitatively implanted in the DOM and it is so convenient to navigate through its elements, to collect the necessary information, that page dynamics can be simply unpredictable.

The function does not have to return a string.characters, number or another function. It can return a full-fledged HTML element, in which there will be the necessary number of elements, with its own event handlers.

Placing such an element on the page, the developer creates a new functionality, which is good in terms of solving the problem and meeting the interests of visitors, but rather difficult in terms of implementation.

Starting such a full-featured development,it's easy to get confused in your own code, in function calls, at the moments when one or another content of one or another part of the page is formed. Before you take this direction of development, it does not hurt to weigh everything well.

About distributed thinking

The developer has to think at the level of all elements of the page, at the level of all events and to have a clear idea how everything actually happens. It is difficult, but this work is worth it.

javascript array functions

In JavaScript, the execution of a function can bepostponed until an event, and there may be many such functions, and even events tend to spread and fall into the “scope” of various handlers.

javascript error the operation is insecure

In this example, somewhere previously calledthe function that initiated the creation of the file navigation menu item. Paging organization is assumed, that is, there are only seven files in the window that can be deleted and processed. You can navigate either by clicking on the file string, or by the arrows on the keyboard, or in blocks of seven lines.

In each case, has its own functions.In other words, in such a simple example it is necessary to write a couple of dozen functions that will respond to various events, and some of these functions will handle various options and situations that do not relate to events at all.

javascript function execution

For example, when deleting a string, the bottom shouldmove up. To do this, you will either need to make a new selection, which is trite and capacious in terms of resources, or recalculate the lines, use the functions of arrays on javascript and elegantly achieve the goal.

Arguments and results of functions

Javascript allows you to cast code to"Fully functional" state. Normally, when the function argument is a function. An option is allowed when the function returns a function. JavaScript takes it quite calmly.

This is a good mechanism, but rather complicated in terms of implementation. Technically, everything is permissible, semantically ensuring the logic of transferring “functionality” is only possible for a qualified developer.

javascript pass function to function

When in javascript function in function - whereverwent, but when a function spawns a function, and that one more, it is quite difficult to keep track of the logic. In fact, the question is not to apply qualifications, the question is to get a safe and correct result.

The care of the developer is clear and simple. There is a task, you need a solution, not an error like “JavaScript error the operation is insecure”, a clean screen or a stop of the entire browser engine.

If the argument is a function, thenthe developer passes a variable with special properties, that is, it is not a number, not a string, not an object. But the use of such an argument can lead to the fact that external variables change and the result of the function execution will be. Depending on the fact that adequate changes will be transferred.

Execution of the generated code

Реализовать исполнение кода, сформированного в process of another code, it is possible by means of “eval”. This is not considered an excellent solution, but you can often not complicate the code with unnecessary functions, but limit yourself to the banal formation of a line of JavaScript code and simply execute it.

function returns javascript function

In this example, the insert string is formed inacting div some information. The diva number and information content are different for different positions, so such a solution in this situation is guaranteed not to provide a “javascript error the operation is insecure” situation, but it will reliably give the desired effect.

The author's example is not a picture

The nuance of the JavaScript paradigm "function in function"

If there is an opportunity to do without excesses, it is better to use it. All of the options listed are good. Of course, in many cases this is the only solution.

The classic example of recursion: factorial computing. It is rather difficult to write an algorithm that will loop, but it’s very simple to go beyond the bounds of the value. Factorial is growing too fast.

However, both recursion and the function that calls another function that can make a valid callback are the norm of things.

For example, a regular table. There may be other tables in the table. Nesting cannot be limited. Writing a set of functions for each table is too much luxury.

There are many such examples, and allthese will be real and urgent tasks, not at all from the field of programming. That is why the problem lies precisely in the fact that you can not do without excesses, the created system of functions, more precisely, its debugging and subsequent reliable work becomes the concern not of JavaScript, but of the developer.

Liked:
0
Popular Posts
Spiritual development
Food
yup