r/javascript • • May 28 '19

What are some JavaScript things beginners don't know?

333 Upvotes

383 comments sorted by

View all comments

152

u/[deleted] May 28 '19

Closure is a concept I see lots of beginners struggle with. You can massively speed up dev time once you grasp how it works in JS.

87

u/Mydrax May 28 '19

Closures are one of the most frequently misunderstood or barely ever undstood concept by beginner Javascript programmers. I personally believe that you will have a lot of "Oh, I see!" moments after you fully understand it. You will be capable of understanding certain things that previously didn't make sense to you because I assure you 100% that you've been using closures without even knowing it!

If you do not understand closures, please read this article, it gives you good understanding of what a normal function is and how it's execution takes place, then explains to you what a closure is. It's big but it will without doubt be an efficient use of time.

23

u/[deleted] May 28 '19

of "Oh, I see!" moments

I understand closure but had very few of these so far

15

u/Mydrax May 28 '19

Well, 2 really awesome use cases of closures I could name off the top of my head are:

  1. When you create function factories, where the outer function can be used to create functions that have similar concerns.

  2. The module pattern, that can be used to create private functions in javascript (where you return a set of functions whose logic is encapsulated).

2

u/turntopage294 May 28 '19

I'd been using 2 without knowing about closures, actually, for a time; I had an 'Oh, I see' moment after investigating when because closures love vaguely familiar

2

u/ScientificBeastMode strongly typed comments May 28 '19

Object factories are cool, and even class factories!

→ More replies (1)
→ More replies (2)
→ More replies (4)

23

u/priceLbear May 28 '19

A few weeks ago I spent some time learning about closure and I understand the concept. How exactly does it speed up dev time though? Do you use it often? If yes, could you describe some use cases I obviously fail to see?

8

u/superluminary May 28 '19

JavaScript treats functions as data. A function is a datatype, just like any other object. This means you can pass it around from place to place just as you would pass a string or a number.

What closure gives you is a stable environment to do this. When you create a function, it gains a closure scope that contains all the variables that the function could see when it was defined. These variables are still available no matter when and how you run your function in the future.

Some example use cases are:

Event handlers. Create them now, and have your code execute them later in another context. They still work.

ES6 modules. All those variables you declare in your module live in the function closure. Import them later, they still work.

There are many others. It's core to the language.

18

u/[deleted] May 28 '19

The better you understand it the more efficient your architecture becomes and less time you spend debugging issues that involve global and local variables not being available to functions etc.

16

u/theycallmeepoch May 28 '19

I would really love to see some examples of this as it's not clear to me yet the benefits other than creating a nested function which has "private" variables. I'm not quite at Closures yet in "Secrets of the Javascript Ninja".

12

u/lostPixels May 28 '19

In my experience working on a codebase with 10,000+ LOC, we never use closures. Every method is generally treated as atomic, where every call just accepts arguments are predictably returns results. Closures are cool but in my opinion can be confusing enough that they will cause some less JS focused developers a lot of issues debugging.

9

u/theycallmeepoch May 28 '19

Yes, I feel like a pure functional programming approach helps minimize the need.

It seems like it was good for making your own getter and setter functions but those are built into JS now...

→ More replies (1)
→ More replies (6)

3

u/el_graveto May 28 '19 edited May 28 '19

Lets say you are making a function that stores properties of pen, like function pen, and properties color, shape, lvl of ink, those properties only exist inside of your function. That is good because you avoid conflict with other parts of your code, but if you need to call those properties or change them? You could do it easily by nesting a function inside your function pen that do whatever you want with those properties, those properties are inside that child function scope but out of the rest of the code scope. You can even call that nested function as argument in another function, it create many possibilities and once you get the hang of it you spend less time coding and have a cleaner code, less prone to bugs, also since those properties (or variables, methods,..) are inside of the function scope you avoid memory leaks.

3

u/theycallmeepoch May 28 '19

What is the advantage to using a function instead of declaring the pencil as an object with its own properties and methods?

4

u/circlebust May 28 '19

You use a function to create such an object if you want an object (ES6 Classes are just syntactic sugar over functions), but the other way around doesn't work if you ever want more than one single pencil in your program.

→ More replies (3)
→ More replies (1)

5

u/kamenjan May 28 '19

I think he meant that your dev time is normal if you understand closures :) You know right away when to use arrow function and when not to, so you avoid frequent and pointless code refactoring on account of not understanding closures :)

11

u/[deleted] May 28 '19

I'd argue arrow functions have nothing to do with closures. Scope and closure are not synonymous.

6

u/[deleted] May 28 '19

If you are a React developer, you are leveraging closures on every single component render even if you're not realizing it.

→ More replies (1)

12

u/razorsyntax May 28 '19

I never understood why we don’t call closures “scope”? What’s the difference between closures and scope in, say, C++ or Java?

20

u/senocular May 28 '19

Scope is where values are defined. Closures are functions which reference one or more scopes. They're related but not the same thing

→ More replies (5)

5

u/eatsomeonion May 28 '19

Closure is part of the function scope property

5

u/Candyvanmanstan May 28 '19

Because closure and scope isn't the same in javascript either. A closure can have access to three scopes. It's own scope, an outer functions scope, and global variables.

2

u/ScientificBeastMode strongly typed comments May 28 '19

You can also take a function as an argument (or from a reference in an outer scope), and apply it inside the current function’s scope, which is how classes are “extended.”

→ More replies (1)
→ More replies (7)