6

For anyone who is expert in both Javascript and C#:

Can we think of JS closures the way we think about Private and Public Properties in c#?

A closure refers to an inner function (public property) that has access to the parent function's variables (private fields), even when the call to the Parent function is completed.

Without the inner function, the scope of the variable in the parent function ceases once the parent function is done executing. Therefore, it seems that one way to access the "private" variables of the parent function is via the "public" inner function which maintains access to the "private" variables of the parent function.

Does this sound about right for a C# guy trying to make sense of Javascript closures?

  • 4
    I'm not really sure what you are asking, but the easiest way to think about closures in terms that a C♯ programmer can understand, would be closures, after all C♯ has them as well. – Jörg W Mittag Aug 1 '16 at 4:11
  • 2
    C# has closures too. They don't exist simply to hide the private variable, it's the other way around: they provide a way to capture the private variable so that you can use it outside its original scope. But you are correct that it's a way to implement encapsulation, since any function scope variables will be private unless they are captured by other functions/closures and exposed publicly. – Lou Aug 1 '16 at 7:28
  • Closures have been called "inside out objects." A class is code that has its own variables. A closure is a variable which has its own code. Ref: perl.com/pub/2002/05/29/closure.html – Tim Grant Aug 1 '16 at 11:43
  • 1
    I tried many times to understand closures when learning JavaScript and always felt like I was missing some secret ingredient that made people go wild over closures (even though I could use them in practice). Then by chance I read Structure and Interpretation of Computer Programs and realized that it was just a fancy term for a the Environment Model of Evaluation, a very simple idea that has existed for a long time. Here is the relevant section: mitpress.mit.edu/sicp/full-text/book/book-Z-H-21.html#%_sec_3.2 – gardenhead Aug 7 '16 at 23:54
18

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 kind of access modifiers.

However, the concept of closures - functions bound together with an "inner scope" - is much more general. It is available in Javascript, C# and other languages as well. Its primary purpose is not to provide something like "private or public properties" - that is only one possible application of a closure. That Wikipedia article has a section "Applications" which contains a list of other possible uses for the concept.

9

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 captures the local variable a.

Closures does not have anything to do with properties in particular, but closures can be used as a way to provide controlled access to variables. In fact, objects can be emulated using closures.

3

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, without having to explicitly create a data structure to hold the info you want to communicate. Consider the following ECMAScript 6 example:

function bestSellingBooks(threshold) {
  return bookList.filter(book => book.sales >= threshold);
}

Here a closure is used as the predicate of the filter function to succinctly communicate a threshold value. We don't think of threshold as a private value of some other entity. We don't want to think of threshold as a private value of some other entity, and that's precisely the point.

Closures are most powerful, and most often used when you want to communicate a value without having to build up a support structure to identify and maintain it. Try to implement the filter predicate above without using closures and you'll see how much rigmarole it avoids. The best closure examples are the ones you barely notice.

  • 1
    did you pick that example specificly for the re-use of the = and > in the opposite order? – Caleth Aug 1 '17 at 10:36
2

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 object with everything it needs to know supplied in the constructor. Now you can implement the no-args methods.

In functional programming this is typically done using closures where a function can use variables from outer scopes and thus implement the no-args requirement.

  • And in C# if you were only using it in one place, you would probably use a closure to avoid the class SomeName { ... } boilerplate – Caleth Aug 1 '17 at 10:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.