What is the need and use of nested functions in JavaScript

I understand what a nested function is, but I donโ€™t understand why we generally need nested functions. Is there a problem that can only be solved with nested functions in JavaScript. All the examples that I see that create a nested function can be encoded without creating a function inside the function and will lead to the same. So, what problem requires the creation of nested functions and can be / effectively solved only with the help of nested functions.

+5
source share
4 answers

The main meaning of nested functions is to create a region. We need nested functions and scope in JavaScript to achieve the following results.

Here is an example of a module that displays the power of encapsulation offered by the nesting function and applications:

var notificationService = (function ($, toastr, undefined) { var _externals = {}, _jqExtend = $.extend; /* * Private Methods */ function _showMessage(params) { params = params || {}; toastr.remove(); if (typeof (params.title) === "undefined") toastr[params.method](params.msg); else toastr[params.method](params.msg, params.title); } /* * Public Members */ _externals.clear = function () { toastr.remove(); }; _externals.showError = function (params) { params = params || {}; _jqExtend(params, { method: "error" }); _showMessage(params); }; _externals.showInfo = function (params) { params = params || {}; _jqExtend(params, { method: "info" }); _showMessage(params); }; _externals.showSuccess = function (params) { params = params || {}; _jqExtend(params, { method: "success" }); _showMessage(params); }; _externals.showWarning = function (params) { params = params || {}; _jqExtend(params, { method: "warning" }); _showMessage(params); }; return _externals; })(jQuery, toastr); 

The above code gives us the ability to control which things to expose. In this particular case, all members attached to the _externals object are exposed to the global namespace through a reference to the notificationService object. Without using scope, internal elements (_jqExtend and _showMessage) will also be attached to the window object and increase the effort required by the browser to resolve identifier references.

+3
source

This is not about efficiency, but about a paradigm. If you are using a script paradigm, you can simply run it off without worries. If you use the classical paradigm, you need to define classes, but you do not need nested functions. However, in scripts and classic, you can use nested functions if you want.

Only if you move on to a functional paradigm, which is the actual โ€œnativeโ€ javascript paradigm, in some cases you will need nested functions.

Until you use functional programming, you will not use the full power of javascript. This is an example factory that uses closures that cannot be done otherwise (there are several other uses for nested functions, not just closures):

 function create(a, b) { function compute() { return a + b; } return { "compute": compute }; } 

User code will do:

 f = create(19, 73); 

Without calculating anything. Let's say that it takes a lot of time to calculate this time, and you don't want to calculate it if necessary. Now you can pass the function f another code that does not belong to you (eG jQuery event handler code):

 $("#id").on("click", function () { console.log(f()); }); 

This f will be executed when you press #id , but there is nothing you can do about it. Arguments a and b already implemented when calling create() .

This means that you can hide information and use contextual coverage. Imagine that the creator and user are two different code files not written by the same person. Or that a and b are secrets that should be kept as such from another code base.

Look at the various words that I sprinkled in the text above to learn more about functional programming.

If you want to know why we can't get around , look at currying and monads , which are fundamental elements of a functional paradigm. You need some mechanisms with which you can perform these basic operations, otherwise the paradigm may not be functional.

+2
source

JavaScript variables can belong to a local or global scope.

Global variables can be made local (private) with closure.

Functions

JS are like variables, so if you want to use nested functions for local use.

Here is a good example.

+1
source

First of all, there are several ways to accomplish a task in programming. But among them, we must choose an efficient and fast method in terms of memory and runtime. When you need to switch from the main program to the subroutine, the time for the context switch is more than just performing the built-in task if the size of the subroutine is smaller .. In general, it is benificial if the size is smaller, otherwise you always have an alternative way. In addition, the nested function has access to the "above" area, them.It will reduce the headache of passing the argument to the function and reuse it somewhere else in the program. All of the variables and functions above are available for this nested function and can update them.

+1
source

Source: https://habr.com/ru/post/1268748/


All Articles