Definition of "closures"

Let me ask one question. It's about closing in JavaScript, but not about how they work.

David Flanagan, in his JavaScript The Definitive Guide 6th Edition, wrote:

... Technically, all JavaScript functions are closures: they are objects, and they have a chain of domains associated with them. ...

It is right? Can I call each function (function object + its scope) "closure"?

Stack closing tags say:

Closing is a first-class function that references (closes) variables from the area in which it was defined. If the closure still exists after its defining scope ends, the variables that it closes will continue to exist.

In JavaScript, every function refers to a variable from the area in which it was defined. So it is still valid.

Question: why do many developers think differently? Is there something wrong with this theory? Could it be used as a general definition?

+6
source share
4 answers

Technically, all functions are closures. But if the function does not refer to any free variables, the closure environment is empty. The distinction between function and closure is interesting only if there are closed variables that must be stored along with the function code. Therefore, they usually refer to functions that do not have access to any free variables as functions, and to those that do closure, so that you know about this difference.

+2
source

This is a difficult term to bind. A function that is simply declared is just a function. What the closure does is call the function. By calling a function, space is allocated for the passed parameters and for the declared local variables.

If the function simply returns some value, and this value is just something simple (for example, nothing at all or just a number or a string), then the closure goes away, and there is nothing interesting about it. However, if some references to parameters or local variables (which are basically the same) "exit" the function, then the closure is the space allocated for local variables, along with the chain of parent spaces - sticks around.

Here is a way that some links could "escape" from the function:

function escape(x, y) { return { x: x, y: y, sum: function() { return x + y; } }; } var foo = escape(10, 20); alert(foo.sum()); // 30 

This object, returned from the function and stored in "foo", will support references to these two parameters. Here is a more interesting example:

 function counter(start, increment) { var current = start; return function() { var returnValue = current; current += increment; return returnValue; }; } var evenNumbers = counter(0, 2); alert(evenNumbers()); // 0 alert(evenNumbers()); // 2 alert(evenNumbers()); // 4 

In this case, the return value is itself a function. This function includes code that refers to the increment parameter and the local current variable.

I would take some problems with combining the concept of closure and the concept of functions, which are first-class objects. These two things are truly separate, although they are synergistic.

As a warning, I am not a formalist by basic personality, and I am really terrified of terminology, so this should probably be showered with downvotes.

+1
source

I will try to answer your question, knowing that you were asked that the closure occurs during the interview (read it from the comments above).

First, I think you should be more specific with "think differently." How exactly?

We can probably say something about this closure of the noop function:

 function() {} 

But it doesn't seem to make sense, since the variables will not be bound to it.

I think that even this example is also not very good to consider:

 function closureDemo() { var localVar = true; } closureDemo(); 

Since its variable will be freed, since there is no way to access it after calling this function, therefore there is no difference between JavaScript and let them speak the C language.

Once again, since you said that you asked what closures are in the interview, I suppose it would be much better to show an example where you can access some local variables through an external function that you get after calling closureDemo() first. how

 function closureDemo() { var localVar = true; window.externalFunc = function() { localVar = !localVar; // this local variable is still alive console.log(localVar); // despite function has been already run, // that is it was closed over the scope } } closureDemo(); externalFunc(); externalFunc(); 

Then, to comment on other cases, and then deduce the most common definition, as it is more likely to force the interviewer to agree with you, rather than quote Flanagan and immediately try to find the page where you read it as the best evidence of your statement or something else . It is likely that your interviewer simply thought that you really did not know what closure was and just read the definition from the book. In any case, I wish you good luck next time.

+1
source

The definition is correct. Closing preserves the area in which he was born.

Consider this simple code:

 getLabelPrinter = function( label) { var labelPrinter = function( value) { document.write(label+": "+value); } return labelPrinter; // this returns a function } priceLabelPrinter = getLabelPrinter('The price is'); quantityLabelPrinter = getLabelPrinter('The quantity is'); priceLabelPrinter(100); quantityLabelPrinter(200); //output: //The price is: 100 The quantity is: 200 

https://jsfiddle.net/twqgeyuq/

0
source

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


All Articles