p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/)
-   BOOK: Professional JavaScript for Web Developers ISBN: 978-0-7645-7908-0 (http://p2p.wrox.com/book-professional-javascript-web-developers-isbn-978-0-7645-7908-0-174/)
-   -   what is the meaning of (function(a){...})(window); (http://p2p.wrox.com/book-professional-javascript-web-developers-isbn-978-0-7645-7908-0/85602-what-meaning-function-window.html)

irProject November 6th, 2011 03:08 PM

what is the meaning of (function(a){...})(window);
 
Hello,
somewhere i've seen this type of javascript codes:


Code:

(function(a){
// some code here ...
})(window);

or

(function(){
// some code here ...
})(jQuery);

or

(function(){
//some code here...
})();

what does these codes do?

irProject November 9th, 2011 01:19 PM

The answer
 
Hello,
I myself found the answer.
these are some ways to write less code. as you know the next line refers to a function:
Code:

function x(arg){ /*some codes here ...*/ }
what about this: ?
Code:

(function x(arg){ /*some codes here ...*/ })
we can call a function by passing parameters like:
Code:

x(3);
so ...
Code:

(function (a){ /* ... */ })(window);
means passing 'window' object as parameter 'a' to the defined function. in the body of this function , 'a' points to 'window'


All times are GMT -4. The time now is 02:49 PM.

Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
2013 John Wiley & Sons, Inc.