dimanche 12 septembre 2021

Javascript Command Pattern Logic

I'm reading Addy Osmani's 'Learning JavaScript Design Patterns' book. More precisely, a similar example is given in the book:

(function(){
  var Manager = {
    foo: function (param) {
      /*...*/
    },
    bar: function (param) {
      /*...*/
    }
  }
})();

The goal is to be able to do this:

Manager.execute("foo",param);

So the book shows this implementation:

Manager.execute = function ( name ) {
  return Manager[name] && Manager[name].apply( Manager, [].slice.call(arguments, 1) );
};

I've read this question on stackoverflow, but there are obvious changes from the example presented in the book. However I cannot understand how CarManager should be initialized and how it can work using that IIFE, since there is a closure with respect to all the functions of the CarManager itself. Why is this IIFE used in the book? Is this a mistake?

Aucun commentaire:

Enregistrer un commentaire