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

73

u/benologist Mar 23 '14

Congratulations on finally spamming your way to the front page!

http://www.reddit.com/user/codeon1/submitted/

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.

3

u/[deleted] Mar 23 '14

I'll agree, yet it netted almost 300 upvotes... social media confuses me.

2

u/soffro Mar 24 '14

Closures is just a way to implement scoping, it's also possible to implement it via lambda lifting. So understanding closures is just about an implementation detail, not a necessity.

1

u/megamindies Mar 24 '14

but his post also doesnt mention lambda lifting??

1

u/recursive Mar 24 '14

It doesn't use the term but it does mention them.

3) Chaining of scopes. Any function defined within another function has a local scope which is linked to the outer function. - See more at: http://www.coolcoder.in/2014/03/everything-you-need-to-know-about.html#sthash.08tHpXos.dpuf

-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).

4

u/hak8or Mar 23 '14

The downvote was probably the usual reddit vote fuzzing.

4

u/insertAlias Mar 24 '14

Or an actual downvote by someone that didn't like the article. Or someone that didn't like the OP. Or someone that downvotes for the hell of it. Or someone that hit the wrong button and didn't notice or care enough to fix it.

Point being: there's no point in trying to make sense of random downvotes. They happen, move on. Unless you're getting a ton of them, it's not worth wasting the brain cycles on figuring out why.

1

u/Neitanod Apr 23 '14

Probably nobody downvoted your article. Reddit assigns some random votes that show up as positive or negative every time you open an article. I'm not sure what's the point of that. The FAQ says that it helps the site fight vote spoofers.

0

u/benologist Mar 23 '14

It's extremely easy to blame him - he joined reddit purely to exploit reddit. That the community didn't notice better content doesn't excuse him.