简体   繁体   中英

What does following javascript code structure mean?

I have come across javascript code with following structure.

var somevar = function(){
    return {
        init: function(){
            function a(){} // no terminator in between functions.
            function b(){}
            function c(){} and so on
            ...
            some jquery declarations
            ...
        } // init ends
    }; // return ends
}(); // somevar ends

Questions are
1. What does this concept is called?
2. How do I call functions inside init function?

UPDATE : Hope close voters understand an importance of this question.

The variable somevar contains the responded object of a directly executed anonymous function, called IIFE . The object has one property, init , what contains a anonymous function too.

The functions inside the init function can only be used on the inside of the anonymous function ( scope ).

 var somevar = function() { return { init: function() { // 'a()' is only available inside of the 'init' function function a() { console.log("init"); } a(); } }; }(); // <-- here the outer function is executed directly // the call of 'init' somevar.init(); 

The structure you are referring to is known as the module pattern.

see module patterns in depth

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM