| Author |
Can somebody tell me the code difference?
|
Colin Zheng
Greenhorn
Joined: Oct 25, 2011
Posts: 14
|
|
why the first one works but the second one doesn't work? What does it make a difference with a "var" in the front?
first one that works:
second one that doesn't work:
|
 |
Bear Bibeault
Author and ninkuma
Marshal
Joined: Jan 10, 2002
Posts: 56233
|
|
I'm assuming the use of jQuery? (It's a good idea to let us know that up front.)
I'm not even sure what you are trying to do here? Why do you have a declaration passed to the jQuery function in the first place? It'll work to establish the function as a ready handler, but it's really really odd.
What is your actual intention?
And to answer the specific question: var is used within non-global scopes to limit the scope of a variable to the current function. Without var, implicitly declared variables will become part of the global scope.
|
[Smart Questions] [JSP FAQ] [Books by Bear] [Bear's FrontMan] [About Bear]
|
 |
Colin Zheng
Greenhorn
Joined: Oct 25, 2011
Posts: 14
|
|
Thanks for the reply, Mr. Bibeault.
Yes, I forgot to mention the use of jQuery.
I am learning Javascript and know that an anonymous function is expected to the the ready handler. I am just curious what it will do if I declare it with a name.
So, the global scope declared function works as a ready handler but a non-global scope declared one doesn't. Can you tell me why? Maybe I can better understand how JavaScript works from finding the answer.
Thanks,
Colin
|
 |
Bear Bibeault
Author and ninkuma
Marshal
Joined: Jan 10, 2002
Posts: 56233
|
|
Colin Zheng wrote:So, the global scope declared function works as a ready handler but a non-global scope declared one doesn't.
It has nothing at all to do with scope.
The problem is that you're trying to use var as a function parameter. No can do.
|
 |
 |
|
|
subject: Can somebody tell me the code difference?
|
|
|