I have a little bit of experience in JavaScript but JavaScript Closure makes me confused.
Asked
Active
Viewed 391 times
-7
-
1Java Closure or JavaScript closures? – Šime Vidas Apr 01 '11 at 18:50
-
Javascript Closures and Javascript – linlin Apr 01 '11 at 18:55
-
do you mean closures, as in the language construct, or Closure, as in the google code project for compiling JavaScript? http://code.google.com/p/closure-compiler/ – Jerod Venema Apr 01 '11 at 19:02
-
@jvenema thanks, I have never seen this link – linlin Apr 01 '11 at 19:20
-
2Zero votes. Zero answers accepted. If you don't get help this might be the reason, so consider improving it. – Shadow The GPT Wizard Apr 01 '11 at 20:07
-
Is this what you are looking for? https://stackoverflow.com/questions/111102/how-do-javascript-closures-work – SavoryBytes Apr 01 '11 at 19:21
1 Answers
2
To understand what the closure is going to do, you need to understand the scope of local variables.
In Java, you can declare a new, distinct local variable in any {} block.
In Javascript, there is different behavior. The interpreter allows declaring a local variable anywhere, but these variables will only be distinct if they are in different functions.
There is no loop scope or loop-defined variables in Javascript, so for(x=0;x<10;x++){ var j=...
behaves exactly like var j; for(x=0; x<10; x++){ j = ..
This is why in Javascript one often wraps one function in another function in order to create a well behaved scope.

Paul
- 26,170
- 12
- 85
- 119
-
you means like write the function in jquery or Is it smiliar jquery, javascript closure and Ajax? – linlin Apr 01 '11 at 19:08
-
-
1jQuery is a library of prewritten JavaScript. A closure is a language construct. Ajax is the technique of using JS to communicate with a webserver. You are comparing a motor parts shop to an engine and to driving to the beach. – Quentin Apr 01 '11 at 19:21