dimanche 24 mars 2019

Can someone explain Javascript classic module pattern and why IIFE is important?

I came across this code, which I don't understand what is going on, and then I removed the IIFE parenthesis and the console.log didn't work anymore, can someone explain what is going on, It is called the classic module pattern.

var foo = (function(){
  var publicAPI = {
    bar: function(){
      publicAPI.baz()
    },
    baz: function(){
      console.log('baz')
    }
  }
  return publicAPI
})()

foo.baz()
foo.bar()

removing the IIFE parenthesis the console.log doesn't work anymore.


var foo = function(){
  var publicAPI = {
    bar: function(){
      publicAPI.baz()
    },
    baz: function(){
      console.log('baz')
    }
  }
  return publicAPI
})


Thank you a lot in advance.

Aucun commentaire:

Enregistrer un commentaire