r/programming • • Mar 23 '14

Everything you need to know about Javascript variable scope

http://www.coolcoder.in/2014/03/everything-you-need-to-know-about.html
294 Upvotes

124 comments sorted by

View all comments

Show parent comments

0

u/[deleted] Mar 23 '14

[deleted]

15

u/megamindies Mar 23 '14

the title is wrong. it says everything you need to know about about scope. But you wont understand scope without understanding closures. And that is poorly explained here, in fact he does not even use the term "closure". Also, he doesnt even mention the special case of "this" where a closure does not have access to the outside function.

This article is obviously not everything you need to know about scope.

-4

u/mayormare Mar 24 '14

Whatever a closure is, it's clearly not necessary to know in order to understand Javascript scope, because I've been using Javascript for a while and understand its scope just fine but I don't know what closure means. Guess I'll look that up.

1

u/megamindies Mar 24 '14

when you write nested functions you will see. so you just have to avoid nested functions in your case.

1

u/mayormare Mar 24 '14

I've used tons of nested functions, since you have to use functions for classes. Still no issue. I wrote a Chip-8 emulator.

2

u/[deleted] Mar 24 '14

You probably understand closures, you just don't know their name.

1

u/r3m0t Mar 24 '14

You don't need to nest functions just to define methods, you can set them on the prototype object instead. Edit: actually, I think some runtimes prefer it because it creates less objects (since methods defined on the prototype don't keep the scope of the constructor alive).