JavaScript AngularJS function hoisting

I have a 'beginner' question. Why does this error out? I call the function in the code, but the function is defined further down.

AngularJS version:

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');
    }
}

http://jsfiddle.net/2fjuJ/4/

But this one works fine:

var myApp = angular.module('myApp',[]);

function MyCtrl($scope) {
    $scope.name = 'Someone';
    $scope.doStuff = function(){
        console.log('did something');
    }

    $scope.doStuff(); 
}

http://jsfiddle.net/2fjuJ/5/

Answer:1

Trying to get the tree layout in D3 to render the child nodes with children closer together. Here is the code: var margin = { top: 20, right: 120, bottom: 20, ...

Trying to get the tree layout in D3 to render the child nodes with children closer together. Here is the code: var margin = { top: 20, right: 120, bottom: 20, ...

I have nav tabs within a modal as shown. I am using the meteor framework. Here is the modal link from sign up button (there is another button called login that links to same modal): <button class=...

I have nav tabs within a modal as shown. I am using the meteor framework. Here is the modal link from sign up button (there is another button called login that links to same modal): <button class=...

I have the following javascript: $('#edit_category').on('click','#btn_save_category_name',function(){ currently_edit.text($('#txt_edit_category').val()); edit_category_name(currently_edit,...

I have the following javascript: $('#edit_category').on('click','#btn_save_category_name',function(){ currently_edit.text($('#txt_edit_category').val()); edit_category_name(currently_edit,...

  1. jquery ajax called twice
  2. jquery ajax called multiple times
  3. jquery ajax error called on 200
  4. jquery ajax fail called on 200
  5. jquery ajax get called twice
  6. jquery ajax success called multiple times
  7. jquery ajax success called too early
  8. jquery ajax not called
  9. jquery ajax done called twice
  10. jquery ajax success called twice
  11. jquery datatable ajax called twice
  12. jquery ajax success not called
  13. jquery ajax done not called
  14. jquery ajax always not called
  15. jquery ajax success not called 200
  16. jquery ajax error not called
  17. jquery ajax fail not called
  18. jquery ajax complete not called
  19. jquery ajax callback not called
  20. jquery ajax fail always called

Let say globalX is a global object variable. Lets define a function that takes that variable and inside another function takes same variable and changes the value inside it. var globalX = []; ...

Let say globalX is a global object variable. Lets define a function that takes that variable and inside another function takes same variable and changes the value inside it. var globalX = []; ...

  1. reference global variable python
  2. reference global variable in function python
  3. reference global variable javascript
  4. reference global variable in class python
  5. reference global variable c++
  6. reference global variable in typescript
  7. reference global variable
  8. reference global variable in method python
  9. reference global variable java
  10. reference global variable r
  11. reference global variables php
  12. python reference global variable inside function
  13. reference to global variable is ambiguous
  14. unresolved reference global variable python
  15. javascript reference global variable in function
  16. postman reference global variable
  17. python reference global variable in module
  18. react reference global variable
  19. angular reference global variable
  20. js reference global variable