开发者

Javascript new way to define object properties and functions? [duplicate]

This question already has answers here: 开发者_如何转开发 Closed 11 years ago.

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?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜