Skip to main content
18 votes

javascript closure = c# properties?

You are mixing up different things here, probably because you have seen examples how closures can be used to simulate the concept of public and private fields in a language which does not have these ...
Doc Brown's user avatar
  • 210k
13 votes
Accepted

Is this extensive usage of closure a known (anti-)pattern? And does it have a name?

This is just using closures. When you're from an OO background building and using a closure ends up feeling like using constructors/factories and methods, respectively. The big difference is you just ...
candied_orange's user avatar
9 votes

javascript closure = c# properties?

C# also has closures. An example: class A { Func<int, int> CreateClosure() { int a = 17; return x => x + a; } } The method returns a closure (the function x => x + a), which ...
JacquesB's user avatar
  • 59.9k
4 votes

Understanding closures

It's not about an outsider being able to change it. Here is a better example which illustrates the usefulnes of closures: var getCodeForUser = (function(username){ var secureCode = CalculateCodeFor(...
Philipp's user avatar
  • 23.4k
3 votes
Accepted

Is there a name for a closure which just returns a value?

In short No, there is no special name Some more explanations Since you capture the variable by value and not by reference, and since PHP uses early binding, this closure will indeed return the value ...
Christophe's user avatar
  • 78.7k
3 votes

Is this extensive usage of closure a known (anti-)pattern? And does it have a name?

As @candied_orange mentioned, what you're doing there is very similar to currying. Its worth learning about. Javascript arrow functions were designed to allow for easy currying. An example of a fully ...
Scotty Jamison's user avatar
3 votes

javascript closure = c# properties?

I really wish blog writers would stop promulgating the privacy aspect of closures. It's a weak usage of them and barely worth mentioning. What's worth knowing about them is the communication aspect, ...
Karl Bielefeldt's user avatar
3 votes

Using a closure to avoid code duplication in Python

I am not a fan of OP's style here. The inner function is not testable and not reusable. In addition, if either the inner or outer function grows significantly long / complex, it becomes a mess to read ...
Sarah Messer's user avatar
2 votes

Are nested closures a good thing?

The ability to have nested functions is a good thing, but obviously any code that is confusing is not a good thing. This kind of code is ubiquitous in the context of asynchronous programming of which ...
Derek Elkins left SE's user avatar
2 votes
Accepted

Is it possible to have nested functions, without having closures, in a lexically scoped language which has first class functions?

I don't think your conclusion is true. function outer1() { const foo = 42; function inner1() {}; } In this case, inner1 is not a closure, but it is a nested function. It simply doesn't use any ...
Jörg W Mittag's user avatar
2 votes

javascript closure = c# properties?

No. If you want to compare to C# a better analogy is constructor parameters. Lets say an api requires you to supply it with a callback with no arguments. In OO you would typically construct an ...
Esben Skov Pedersen's user avatar
2 votes

Is a lambda expression something more than an anonymous inner class with a single method?

Joachim Sauer already did a good job answering you question but only hinted at something I regard as important. Since Lambdas are no classes they are also not compiled as such. All anonymous inner ...
Christoph Grimmer's user avatar
2 votes

Is this extensive usage of closure a known (anti-)pattern? And does it have a name?

Functions are objects. The have but one entry point instead of multiple methods but this limitation does not change their basic nature. Closures are configurable objects. When closures capture ...
yeoman's user avatar
  • 383
2 votes
Accepted

Purpose of Closure Style in Laravel/PHP?

The Laravel route directives actually do accept a string as the second parameter: Route::get('/user', 'UserController@index'); This is parsed by the Laravel router to call the desired action in the ...
HorusKol's user avatar
  • 4,131
1 vote

Are nested closures a good thing?

You can try something like this: func signIn(email: String, password: String) -> User? { var user: User? = nil DispatchQueue.global().async { let semaphore = DispatchSemaphore(...
Sergey Petrachkov's user avatar
1 vote
Accepted

Understanding closures

In your particular contrived example, there is no difference at all. If you only ever use a variable by value, and assign it a compile-time value, it matters not whether you use a closure or not. In ...
Cort Ammon's user avatar
  • 11.6k
1 vote

Understanding closures

People often get excited talking about what closures can do, and they forget to talk about what you should do with closures. Your particular example builds on the previous example, which showed ...
Karl Bielefeldt's user avatar

Only top scored, non community-wiki answers of a minimum length are eligible