javascript变量未被函数

时间:2015-04-26 22:14:59

标签: javascript

我想知道为什么变量" gists"没有在箭头上的功能中修改。它在函数之外声明,因此它应该是全局的。我尝试将变量作为参数添加到函数中,但这也不起作用。但是当我在函数外部访问变量时,它并没有显示它已被修改。有这个原因吗?

        var gists; // = [];

        function getGists()
        {

            var request = new XMLHttpRequest();

            if(!request)
            {
                throw 'Unable to create HttpRequest.';
            }

            var url = 'https://api.github.com/gists/public';
            //var url = 'https://api.github.com/gists/users/:smithjoe123/gists';




            request.onreadystatechange = function()
            {

                if(this.readyState == 4)
                {


                    //console.log(this.responseText);


                    var txt = this.responseText.trim("\"");

                    gists = JSON.parse(txt);

                    gists = 5544; //<<<<<<----------VARIABLE HERE NOT MODIFIED

                    console.log("xxyyiii<<<<<<<<!!");









                }
            };

            request.open('GET', url);

            request.send();

        }

        getGists();

        console.log(gists); //here it does not show that it has been modified

        //console.log(gists[0].user);

0 个答案:

没有答案