• 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
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Javascript questions from Bootstrap

 
Ranch Hand
Posts: 115
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, I've read through most of W3School lessons for Javascript and jQuery but I'm still unable to understand the following.



What is "+function ($)" doing? On StackExchange someone says it's loading immediately? All the functions are declared like that in the file. Why do you want to load everything?

Why does the function end with "}(jQuery);"? Not sure what the extra "(jQuery)" doing after the closing curly brace?
 
Sheriff
Posts: 67746
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
The "+' notation forces the following function to be evaluated as an expression, and the trailing (jQuery) causes the function to be executed with jQuery as its argument. Note that within the function, this is assigned to $ via the parameter list of the function.

This is called an "immediate function" because it is declared and executed at the same time ("immediately").

The notation more commonly used for this is:but some people prefer the "+" notation.
 
Mike Cheung
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bear Bibeault wrote:The "+' notation forces the following function to be evaluated as an expression, and the trailing (jQuery) causes the function to be executed with jQuery as its argument. Note that within the function, this is assigned to $ via the parameter list of the function.

This is called an "immediate function" because it is declared and executed at the same time ("immediately").

The notation more commonly used for this is:but some people prefer the "+" notation.


Ah I see and thanks for highlighting the term "immediate function", which I have been able to look up successfully on Google and found the following excellent article.
http://blog.kevinchisholm.com/javascript/javascript-immediate-functions-basics/
 
Bear Bibeault
Sheriff
Posts: 67746
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
You can also find extensive discussion in chapters 3 and 4 of Secrets of the JavaScript Ninja

Immediate functions are very useful for creating enclosed scopes.
 
reply
    Bookmark Topic Watch Topic
  • New Topic