javascript - Issue with Global Variables from within functions in JS -


i have following script:

    <script>                 var xmlhttp = new xmlhttprequest();                 var myapp = angular.module("myapp", []);                  xmlhttp.onreadystatechange = function () {                     if (this.readystate == 4 && this.status == 200) {                         response = this.responsetext;                         var myobj = json.parse(response);                         console.log(myobj);                      }                 };                  xmlhttp.open("get", "functions.php?action=getuserdata&id=" + document.getelementbyid('userid').innerhtml, true);                 xmlhttp.send();                  console.log(myobj);                  myapp.controller("myctrl", function ($scope) {                     $scope.firstname = myobj[0].firstname;                     $scope.lastname = myobj[0].lastname;                     $scope.email = myobj[0].email;                     $scope.cell = myobj[0].cell;                     $scope.domainname = myobj[0].domainname;                     $scope.tfamethod = myobj[0].tfamethod;                     $scope.said = myobj[0].said;                 });     </script> 

now understanding 'var' keyword, assigns variable globally accessible anywhere within window. set var within onreadystatechange function, , i'm able see when doing first console.log, 'undefined' when log again outside function. understanding of var incorrect? if so, how fix code able access myobj outside function?

two issues code:

var

now understanding 'var' keyword, assigns variable globally accessible anywhere within window.

that understanding incorrect. var declares variable within function var appears. declare global variable, declare outside of functions.

or better yet, don't. global namespace on browsers very crowded. instead, wrap of code in scoping function , put "globals" there. they'll accessible of code, without being global.

timing

separately: can't use myobj had in code, because won't have been assigned yet, regardless of it's declared. see this question's answers why.


so here's code scoping function, , using myobj in correct place (inside callback); also, since ever use first entry in returned array, access entry once instead of repeatedly:

(function() {     var xmlhttp = new xmlhttprequest();     var myapp = angular.module("myapp", []);      xmlhttp.onreadystatechange = function() {         if (this.readystate == 4 && this.status == 200) {             response = this.responsetext;             var myobj = json.parse(response)[0];           // note [0]             myapp.controller("myctrl", function($scope) {                 $scope.firstname = myobj.firstname;                 $scope.lastname = myobj.lastname;                 $scope.email = myobj.email;                 $scope.cell = myobj.cell;                 $scope.domainname = myobj.domainname;                 $scope.tfamethod = myobj.tfamethod;                 $scope.said = myobj.said;             });         }     };      xmlhttp.open("get", "functions.php?action=getuserdata&id=" + document.getelementbyid('userid').innerhtml, true);     xmlhttp.send(); })(); 

Comments

Popular posts from this blog

Sort a complex associative array in PHP -

vb.net - How to ignore if a cell is empty nothing -

recursion - Can every recursive algorithm be improved with dynamic programming? -