简体   繁体   中英

Module pattern's anonymous closures

In every article about module pattern I see defining modules in a following way:

var foobar = (function() {
   // code
})();

What is the difference between this and:

var foobar = function() {
   // code
};

? Both seem to be working in the same way.

In the first case, foobar returns the result of the anonymous function call, whereas in the second one foobar IS the anonymous function.

This all means they aren't the same thing at all, and dont work the same way.

They shouldn't seem to be working the same way.

One defines a function and calls it, one defines a function.

In the first case, foobar is set to whatever the function returns.

In the second, foobar is the function.

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