Javascript new way to define object properties and functions? [duplicate]
Possible Duplicate:
What is the purpose of wrapping whole Javascript files in anonymous functions like “(function(){ … })()”?
I have stumbled upon many times this unusual way of coding, and it seems to be becoming popular.
(function (variable) {
// properties or methods go here
// some more stuff
})(variable)
It's hard for me to even research it, because i don't even know how it's called. i have worked with it with jquery with but i still don't know how it works.
example:
(function ($) {
...
// code to manipulate the dom
function init() {
.....
}
$(document).ready(function () {
init();
});
})(jQuery);
I've only used it because i was updating some code made by another developer.
Is there any advantage to coding like this? Is there a place where i can read more about it? If anybody understand my question, it will be nice to see some articles that talk about this, or maybe you have some insight on how to make your own.
Thank You
Ibu
Its called a self invoking anonymous function. Look at this thread for more details about how it works and why its used,
Why do you need to invoke an anonymous function on the same line?
精彩评论