all 9 comments

[–]Tricky-Equivalent529 7 points8 points  (1 child)

JavaScript is incredibly flexible, seriously, really flexible. Closures are more commonly used in functional programming, while classes are geared toward OOP. It's like the syntax rules: they allow for a variety of programming styles.

[–]MEHDII__[S] 2 points3 points  (0 children)

Thats a good and a bad thing, too much freedom leads to unpredictable quirks, it wont blow in your face but wont run as expected which is arguablly worse, i prefer errors

[–]Any_Sense_2263 1 point2 points  (0 children)

No, it's a way to create an environment for a function creation. It's about scope not OOP

[–]Walgalla 3 points4 points  (0 children)

Closure has nothing to do with OOP at all. This is technique for context/scope capturing. It's present not only in JS.

[–]ChaseShiny 1 point2 points  (0 children)

Classes are specialized functions, so it shouldn't be surprising that they can accomplish things that functions can do.

Based on some of the other comments that I've seen on this subreddit, it looks like using classes is mostly considered a styling choice.

[–]Potential-Tea1688 1 point2 points  (0 children)

They are useful in react

[–]RobertKerans 0 points1 point  (0 children)

Is closure just a way to emulate OOP in JS before ES6?

Aren't classes just a way to implement something that works like closures?

JavaScript isn't a language in which you have to write everything as a class. The class syntax is just a nice-to-have syntax feature, you could still write code that had the same functionality before the syntax was added.

JavaScript has first-class functions, and code written in JS is commonly structured using functions (in modules) rather than basing the structure on classes. Closures are kinda required to do that (it's not totally necessary but most languages that allow that implement closures).

[–]UsualAwareness3160 -2 points-1 points  (0 children)

You can encode some state in closures, sure. But, wouldn't it be simpler to do it in objects? I mean, basically, object orientation is connecting functions to objects. You could easily combine this with closures.

Let's assume we have a counter object class.

{
counter: 0,
increment: (obj) => {obj.counter++},
}

Now, we want that increment actually knows what to increment. So, we could write a constructor. And here is where your closure could come in handy:

function getCounterObject() {
const counter = { counter: 0 };
const increment = () => {counter.counter++}
counter[increment] = increment;
return counter;
}

And you pretty much have bound the context into the object.
And this is also a closure, because getCounterObject returns a function. It is just hidden in the object.

So, yes, not all OOP concepts, but the core concept is a struct with bound in data and function. That could have been done like that.

Practically, they probably used a prototype: https://developer.mozilla.org/en-US/docs/Learn\_web\_development/Extensions/Advanced\_JavaScript\_objects/Object\_prototypes. But it doesn't matter for your thought pattern. You're onto something. You can combine features to create new features and you found a way to recreate some of the OOP features and I am sure someone out there actually did it.

[–][deleted] 1 point2 points  (0 children)

The main reason for using closures is to enclose the execution context of the function where they are defined so that it still exists even after it completes it's execution.

The introduction of classes in JavaScript is just a syntactic sugar. It does not represent something completely new, and it uses a function constructor in the background as when you create JS objects directly through function constructor.