Revealing module pattern

which is better in the revealing module patterns?

var nameSpace = (function () {
   function init(){
      //code here
   }
   return {
      //code here
   }
})();

var nameSpace = function () {
   var init = function(){
      //code here
   }

   return {
      //code here
   }
}

is is better to have funciton blah() or to var blah = function ?
are there any benefits either way?

function blah() is a function declaration. You can invoke the function even before it was defined. var blah = function is a function expression. The function doesn’t exist until that particular line is executed. When you mix and match the two, then you get one of those quiz questions that tries to trip you up with JavaScript’s weirdness. :slight_smile:

Sticking with function expressions is probably the saner choice.

with expressions though don’t you run into issues with hoisting?

It’s actually function declarations that are hoisted.

seemed to got mysefl confused between the two.

var blah = function() - is an expression
function blah() - is a declaration

Is this right?

This is a function declaration:

function foo() { ... }

This is a named function expression:

var foo = function bar() { ... }

This is an anonymous function expression:

var foo = function(){ ... }

AFAIK, because of hoisting, the function declaration can be called both before and after its definition, whereas the expressions can only be called after creation.