aspose file tools*
The moose likes HTML, CSS and JavaScript and the fly likes Can somebody tell me the code difference? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "Can somebody tell me the code difference?" Watch "Can somebody tell me the code difference?" New topic
Author

Can somebody tell me the code difference?

Colin Zheng
Greenhorn

Joined: Oct 25, 2011
Posts: 15
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: 61413
    
  67

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.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Colin Zheng
Greenhorn

Joined: Oct 25, 2011
Posts: 15
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: 61413
    
  67

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Can somebody tell me the code difference?