title: AngularJS/ JS How do I access variables outside my function?
body: <p>I'm trying to access the variable np defined from within a function block; however, I am running into some issues when I call <code>this.items.push(plumbers)</code>. I get <code>TypeError: Cannot call method push of undefined</code>  </p>
  
  <pre><code>myApp.factory('np', function($resource, nearbyPlumbers){
    var np = function(){
      this.items = [];
      this.busy = false;
      this.limit = 5;
      this.offset = 0;
    };
  
    np.prototype.nextPage = function(){
      if (this.busy) return;
      this.busy = true;
  
      var temp;
  
      nearbyPlumbers.nearby({lat: -37.746129599999996, lng: 144.9119861}, function(data){
        angular.forEach(data, function(plumber){
          alert('yay');
          //this.items.push(plumber);
          console.log(plumber);
          console.log(this.items); // &lt;--- This wont work. How do I access this.items
        });
      });
    };
    return np;
  });
  </code></pre>