title: AngularJS function hoisting
body: <p>I have a 'beginner' question. Why does this error out? I call the function in the code, but the function is defined further down.</p>
  
  <p>AngularJS version:</p>
  
  <pre><code>var myApp = angular.module('myApp',[]);
  
  function MyCtrl($scope) {
      $scope.name = 'Someone';
      $scope.doStuff(); // run the defined function, but errors out
  
      $scope.doStuff= function(){  // function definition
          console.log('did something');
      }
  }
  </code></pre>
  
  <p><a href="http://jsfiddle.net/2fjuJ/4/" rel="nofollow">http://jsfiddle.net/2fjuJ/4/</a></p>
  
  <p>But this one works fine:</p>
  
  <pre><code>var myApp = angular.module('myApp',[]);
  
  function MyCtrl($scope) {
      $scope.name = 'Someone';
      $scope.doStuff = function(){
          console.log('did something');
      }
  
      $scope.doStuff(); 
  }
  </code></pre>
  
  <p><a href="http://jsfiddle.net/2fjuJ/5/" rel="nofollow">http://jsfiddle.net/2fjuJ/5/</a></p>