Skip to content

Comment on Closures for class/module privacy considered harmful

Comments

You cannot know ahead of time how someone else will want to modify your code

And this is exactly why I make implementation private. Otherwise my implementation becomes my public interface and I can't change it without breaking somebody else's patch.

If you’ve got a tricky interaction between a few private functions it’s very helpful to be able to test them!

Then refactor those tricky functions into another class with testable interface.

    var SomeModule = (function(trickyFunctionality) {

      function publicFunction() {
        trickyFunctionality.doTheTrick()
      }

      return {
        publicFunction: publicFunction
      }

    })(new Tricky())
AboutSource Built by g1lg1l

Hackerly is an independent reader for Hacker News, built on the public HN API. Not affiliated with Y Combinator.