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
  • 201k
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
  • 57.4k
7 votes
Accepted

Why closure declaration syntax in Swift is different from function declaration

Swift chose lambda-style syntax for their function parameters in ordinary functions as well. The only difference between an ordinary function and a closed-over one is the positioning of the braces ...
Robert Harvey's user avatar
7 votes
Accepted

What is the use case for shadowing variables?

The issue with the way you describe it (I don't know coffeescript myself) is that it makes it too easy to break existing code without realizing it. Let's say you have this code: bar = 5 // 100 lines ...
Gort the Robot's user avatar
5 votes

Are a class's methods a type of closure?

Yes, but global variables have nothing to do with it. A closure is a function that encloses state from an outer scope. This enclosed state is persistent, and more than one function can enclose the ...
Mason Wheeler's user avatar
5 votes
Accepted

What is the most generic way to provide a variable amount of outputs from a Rust function?

I don't think that the use of iterators you propose is an abuse of iterators at all. It's perfectly acceptable for iterators to perform computation and for them to have significant amounts of state. ...
Paul Stansifer's user avatar
4 votes
Accepted

What is the difference between explicit parameters and closures

From the compiler's point of view, there's very little difference between a closed over variable and a function argument. They are just given to the function's symbol table at different times. In ...
Karl Bielefeldt's user avatar
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.2k
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
  • 74.8k
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

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

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
Accepted

Are a class's methods a type of closure?

Are a class's methods a type of closure? No. A closure is a very specific type of function. As Wikipedia says, they implement lexical scoping. Closures In Python, such a closure would look like ...
Aaron Hall's user avatar
  • 5,915
2 votes

A JavaScript IIFE prevents leaks to the global space as a closure? Is that the correct concept?

I think it is a local scope, or an anonymous local scope, that is shield any local variables from leaking to the global scope. Yes, that's precisely what it is. And because JavaScript doesn't ...
Lightness Races in Orbit's user avatar
2 votes

What is the difference between explicit parameters and closures

Afaic, at least in C#, at least sometimes behind the scenes a class is generated to store captured variables in it's fields. It is a significant overhead compared to stack allocation to pass parameter ...
Artem K.'s user avatar
  • 121
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 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
  • 373
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
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
  • 10.9k
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
1 vote
Accepted

Javascript Closures to Separate Class

It sounds like you've discovered that you can use javascript closures to create classes. Doing this you can treat javascript as an object oriented language. But is this wise? Look at java 8. Java ...
candied_orange's user avatar
1 vote

What is the use case for shadowing variables?

The backlash towards CoffeeScript specifically is likely because CoffeeScript is supposed to be a language that is similar to Javascript; it is advertised as a variation or extension of Javascript. ...
Jeffrey Sweeney's user avatar
1 vote

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
1 vote

Are a class's methods a type of closure?

I think what you're asking is whether static methods and static variables of the same class can be considered as a (perhaps poor person's) closure construct. While it has some resemblance to the ...
Erik Eidt's user avatar
  • 33.4k

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