title: Calling a function in AngularJS service from the same service?
body: <p>I have an AngularJS service defined in the following way</p>
  
  <pre><code>angular.module('myService').service('myService', function() {
    this.publicFunction(param) {
      ...
    };
  
    this.anotherPublicFunction(param) {
      // how to call publicFunction(param)?
      ...
    };
  });    
  </code></pre>
  
  <p>and I would like to call the first function both from outside the service (which works fine by <code>myService.publicFunction(xxx)</code>) and from another function in the same service, i.e. <code>anotherPublicFunction</code>. <del>Neither one of <code>this.publicFunction(param)</code> or <code>myService.publicFunction(param)</code> won't work from within the second function, and I can understand that.</del></p>
  
  <hr>
  
  <p><strong>EDIT:</strong><br />
  <em>Actually the whole problem was caused by something you can't reproduce with my example alone. I passed the second function as a callback parameter to another function in a separate controller, and when it is called, the reference to <code>this</code> doesn't work.</em></p>
  
  <p><em>E.g.</em></p>
  
  <pre><code>anotherService.someCall('a', 123, myService.anotherPublicFunction);
  </code></pre>
  
  <p><em>fails inside <code>anotherPublicFunction</code> because <code>this</code> can't be resolved.</em></p>
  
  <p><em>I've written a Plunker to show the problem:</em>
  <a href="http://plnkr.co/edit/rrRs9xnZTNInDVdapiqF?p=info" rel="noreferrer">http://plnkr.co/edit/rrRs9xnZTNInDVdapiqF?p=info</a></p>
  
  <p><em>(I'll still leave the question here in case it will help someone else.)</em></p>
  
  <hr>
  
  <p>I know I could get around the problem by using a reference to the service or the first function like this</p>
  
  <pre><code>var ms = this;
  this.anotherPublicFunction(param) {
    ms.publicFunction(param);
    ...
  };
  </code></pre>
  
  <p>or this</p>
  
  <pre><code>var pf = this.publicFunction;
  this.anotherPublicFunction(param) {
    pf(param);
    ...
  };
  </code></pre>
  
  <p>but both seem like dirty hacks.</p>
  
  <p><strong>Is there a good way to call the first function from the second one in this case? Or am I doing something totally wrong in the first place to have a service like this?</strong></p>
  
  <p>I found these questions with good answers:</p>
  
  <ul>
  <li><a href="https://stackoverflow.com/questions/16753825/angularjs-share-functions-inside-service">Angularjs share functions inside service</a></li>
  <li><a href="https://stackoverflow.com/questions/18853797/angularjs-call-an-internal-service-function-from-self">AngularJs call an internal service function from self</a></li>
  </ul>
  
  <p>but they differ from my problem since one of them has a separate, internal function that was to be called, and the other one was using a factory instead of a service.</p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>After posting this I immediately realized I could also do this:</p>
  
  <pre><code>var actualWork = function(param) {
    ...
  }
  
  this.publicFunction(param) {
    actualWork(param);
  };
  
  this.anotherPublicFunction(param) {
    actualWork(param);
    ...
  };
  </code></pre>
  
  <p>which doesn't seem quite as bad as the other options so far... Are there better approaches?</p>