• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Liutauras Vilda
  • Paul Clapham
Sheriffs:
  • paul wheaton
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Piet Souris
Bartenders:
  • Mike London

Can somebody tell me the code difference?

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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:
 
Sheriff
Posts: 67695
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Colin Zheng
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 67695
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic