Most voted "closures" questions
Enclosure is a function that references free variables in the lexical context. An enclosure usually occurs when one function is declared within the body of another, and the inner function references local variables of the outer function.
Learn more…27 questions
Sort by count of
-
124
votes5
answers9611
viewsHow do Closures work in Javascript?
I always wanted to know how Closures work in Javascript, I’ve read some settings but never understood very well. Could you give me a simple, objective but content explanation?
-
38
votes2
answers2228
viewsWhat is the difference between a lambda expression, a closure and a delegate?
From what I’ve been reading the three concepts are quite similar, but I was confused as to the clear and exact definition of them. As far as I know, a lambda expression for being understood as a…
-
9
votes2
answers647
viewsHow to use Ruby blocks
I’m having a hard time understanding blocks in Ruby. Could someone explain to me why we use it?
-
9
votes1
answer191
viewsWhat does the [&] operator mean before a function?
I’m trying to read the code of a function that’s defined like this: auto loop = [&](int ntensor, char** data, const int64_t* strides, int64_t n) { ... }; What does the [&] before function?…
-
8
votes3
answers243
views"Unclosure" a closure
I understand the great powers and the great consequences of the closures. But suppose I have the following code... function foo () { var x = 1; function bar () { return x++; } return bar; } ... And…
-
7
votes1
answer114
viewsIs every anonymous function a closure?
Every anonymous function is considered a closure or only those that refer to the context where they were created are? I wish I knew that to distinguish them correctly. I know the concepts and the…
-
6
votes1
answer58
viewsHow does an annonymous innerclass arrow a private instance variable?
Since Java does not actually have closures, but emulates behavior with a technique using Inner classes, the following code: class Test{ private int myField; private void doSomething(){…
-
5
votes1
answer108
viewsHow to serialize an Exception with closure
I am trying to serialize the error generated exceptions to record a log with all the information, but the problem is that some exceptions may come with a closure within the trace and fire the…
-
5
votes2
answers180
viewsUse Static Closures or Closures in PHP?
As of PHP 5.4, the Closures (Or funções anônimas), when declared within the context of a class method (other than static methods), automatically inherit to "within" the $this as a reference to the…
-
4
votes1
answer365
viewsWhat is the benefit and in what cases use closures in PHP?
Ex.: Why use a closure for this function: We could do the same thing without a closure, but why use this class? public function getTotal($tax) { $total = 0.00; $callback = function ($quantity,…
-
4
votes3
answers857
viewsJavascript Method Overload Function
I’m reading a book called "Secrets of Ninja Javascript" and in it, I came across a method overload function. So far so good, I understood what she does, I understood that she makes depending on the…
-
3
votes1
answer181
viewsWhat are the variables/functions Anonimas and closures for? And what are they for? How to use?
I think most of the question has already been asked in the title. More specifically, I wanted real examples of using these techniques and the relationship between them. Note: I read other related…
-
3
votes2
answers374
viewsJava lambdas are equivalent to Javascript Closures?
I started studying functional programming and I’m a little confused about these two items. My point is: Java lambdas are equivalent to Javascript Closures? If they are not, what is the difference…
javascript java lambda-expressions functional-programming closuresasked 7 years, 5 months ago Bruno Peres 1,202 -
3
votes3
answers672
viewsWhat is the advantage of using Function(window, Document, Undefined)
I noticed a long time ago that many libraries are using something like: (function(window, document, undefined){ //exemplo de script console.log(window, document); })(window, document); or…
-
3
votes1
answer45
viewsUse closure as return of a function
I need to use a variable of a closure as a return to the function to which this closure is nestled. How can I do this? response must be the return of ajaxRequest(): function ajaxRequest(type, url) {…
-
3
votes1
answer101
viewsAre there advantages to using closures to maintain state rather than class?
It is very common to use classes to encapsulate state and some methods that modify it, such as this counter: class Counter { #val = 0; increment(step = 1) { this.#val += step; } retrieve() { return…
-
2
votes3
answers861
viewsRun php code within a variable
Good evening, everyone There is the possibility to rotate this: <?php $variavel = 'foreach($v1 as $v2){echo $v2;};'; echo $variavel; ?> Or is there any way to run a class within a variable (a…
-
2
votes1
answer96
viewsScope of PHP variable
So, I’ve been banging my head for a while now with a basic question of scope, but I couldn’t figure out why. In the function below, I am trying to access a position of the object that is running at…
-
2
votes1
answer136
viewsHow do closures work on Swift?
Perhaps this is one of the most difficult concepts to understand for those who are beginning in language. I’ve seen some definitions but so far I can’t understand. Could someone give an explanation…
-
2
votes0
answers69
viewsAre there advantages or disadvantages between using closures or classes to achieve Javascript dependency injection?
Let’s say I have a function that depends on a logger to work. To follow the "good practice" and achieve a certain level of decoupling, I decide to make use of dependency injection so that this…
javascript classes software-architecture dependency-injection closuresasked 3 years, 9 months ago Luiz Felipe 32,886 -
1
votes1
answer63
viewsIs it possible to serialize closures in PHP?
It is possible to serialize closures in PHP (even if it is not natively)? Because PHP generates a Fatal Error while trying to do so: $func = function ($a, $b) { return $a + $b; }; serialize($func);…
-
1
votes1
answer86
viewsDifference between closures and functions
Closures is a Function in Groovy ? There is some difference between them?
-
1
votes1
answer137
viewsDoubt about closures, function inside loop
I’m studying about closures in Javascript based on the book "You Don’t Know Js" by Kyle Simpson. I understood the concept of closure, that he can "hold" the reference to the lexical scope outside…
-
0
votes2
answers150
viewsIs it possible for a Closure (Swift) to capture reference of the instance of the object that executes it?
I am passing a closure for the property of an object, and within the closure would need to make a reference to the instance of the object that will run the closure. Example: typealias validator :…
-
0
votes1
answer34
viewsRecursions in closures
# -*- coding: utf-8 -*- def memoize(limit, *, message = 'Limit exceded'): count = 0 def inner(func): cache = {} def wrapped(number): nonlocal count if count < limit: if number not in cache:…
-
0
votes1
answer93
viewsBasic closures
Sirs, I’ve been studying Python for about four months and now I’m entering the most complex concepts of language. I’m having a little trouble assimilating decoradores but I understood that first I…
-
0
votes0
answers15
viewsIs creating module using closure function a good practice?
It is known that closure is used to make variables within a function private and persistent. We assume I use closure to modulate smaller functions, would be considered good practice? If not, what…