title: Encapsulation Pattern to Use with AngularJS
body: <p>I need to create smart objects/functions in Javascript to use with my AngularJS app. What pattern should I use for this? I currently am using the generic JavaScript 'module' pattern which I researched, but I am wondering if I should be modeling these object/functions (below) in an AngularJS-way. Perhaps as 'services'?</p>
  
  <p>I come from a Java background which makes me a little uncomfortable calling a object with helper methods a 'service.' But I may need to adjust my definitions for JavaScript/AngularJS.</p>
  
  <p>The app is a basic grading system. The two main objects/functions are below:</p>
  
  <p><strong>LessonScoreCard</strong></p>
  
  <pre><code>/* 
   * LessonScoreCard
   * 
   * Is a "module" that keeps track of the score a user has for a given lesson
   * and whether or not for a given question there are more correct answers 
   * that can be made.
   */
  
  var lessonScoreCard = (function() {
  
      //Map of questions to scores
      var privateQuestionNumberToScoreMap = {};
  
      //API to be used by external clients
      var publicAPI = {
  
          //A public function utilizing privates
          setScore: function(questionNumber, score) {
              privateQuestionNumberToScoreMap[ questionNumber ] = score;
          },
  
          //Sum the total score
          getTotalScore: function( ){
              var totalScore = 0;
              for( var questionNumber in privateQuestionNumberToScoreMap ){
                  totalScore += privateQuestionNumberToScoreMap[questionNumber];
              }
          }
      };
  
      return publicAPI;
  })();
  </code></pre>
  
  <p><strong>Answer Key</strong></p>
  
  <pre><code>/* 
   * AnswerKey
   * 
   * Is a "module" that takes an answer key and performs functions
   * related to it.
   */
  
  var answerKey = (function() {
  
      //Set of right answers
      var privateQuestionNumberToAnswerArrayMap;
  
      //API to be used by external clients
      var publicAPI = {
          setAnswerKey: function(answerKeyModel) {
              privateQuestionNumberToAnswerArrayMap = answerKeyModel;
          },
          // A public function utilizing privates
          isCorrect: function(question, answer) {
              var isCorrect = false;
  
              var answerArray = privateQuestionNumberToAnswerArrayMap[ question ];
              if (answerArray.indexOf(answer) !== -1) {
                  isCorrect = true;
              }
              return isCorrect;
          },
  
          getAnswerCount: function( question ){
              var answerArray = privateQuestionNumberToAnswerArrayMap[ question ];
              return answerArray.length;
          }
  
      };
  
      return publicAPI;
  
  })();
  </code></pre>
  
  <p><strong>Sample JSON Answer Key Model</strong> </p>
  
  <pre><code>    {
          1: [1, 3],
          2: [4]
      }
  </code></pre>