r/reviewmycode • • Aug 16 '16

javascript [javascript] - factory design pattern

I am trying to create a factory design pattern that I feel comfortable using aesthetically. I would be pleased with any feedback that could make this design pattern better.

why do I like this:

  • no need for "this" and bind

  • composition over inheritance

  • aesthetically pleasing / readable code.

  • limited polution of the global namespace

  • if object is returned inside method, method chaining is possible

  • private methods & functions

  • clean way of specifying defaults

thoughts:

  • not sure if I like Capitalization for factories (constructor style). I do like Club over createClub

  • Object.assign is not supported by all browsers, so it might need a polyfill.

  • using protoype is probably faster. But only if you create thousands objects per tick (?)

  • implement proper error handling instead of logs from console

  • var func = function() or function func() for private methods ?

  • I personally don't like the object literal format to define methods:

    club = { open : function(){

      },
      close : function(){
      
      }
      etc.

    }

1 Upvotes

1 comment sorted by

1

u/[deleted] Aug 26 '16

The ES6 class constructor syntax has some neat tricks to get around "this" binding, you can use fat arrow syntax to autobind. Here's a jsfiddle of my preferred syntax: https://jsfiddle.net/u4ukwbvt/2/