Created
July 17, 2013 16:09
-
-
Save i-Hun/6022001 to your computer and use it in GitHub Desktop.
JS: анонимные самовызывающиеся функции
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
http://stackoverflow.com/questions/1122690/jquery-and-questions/1122740#1122740 | |
Let's rewrite this code a little bit to understand what's going on. | |
function complicatedFunction($) { | |
// the document.ready call goes here. | |
} | |
Next, how would you call this function? | |
complicatedFunction(someObject); | |
So inside the complicatedFunction $ refers to someObject. Agree? | |
If you write | |
complicatedFunction(jQuery); | |
Then inside the function, $ refers to the jQuery object. So everything inside, complicatedFunction can use '$' as like how a normal jQuery user would do. | |
Coming back to the original code, if we decide to not name this function i.e. make it anonymous, you can visualize the code like, | |
(function($) { })(jQuery); | |
You are creating an anonymous function, taking one argument named $. You immediately call this anonymous function passing it the jQuery object. This way, you don't modify the global $ object but all the code inside your anonymous function works like $ was always available. Cool, isn't it? :) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment