为什么我的数组在此代码中通过引用传递?

时间:2015-11-01 20:53:42

标签: javascript jquery arrays

我有一个名为问题的空数组,我使用$.get方法将对象添加到数组中。在$.get内,数组充满了对象并且工作正常。但是一旦在$.get之外,数组会再次变空,因为array.length会返回0

var questions=[];
var input;
$(document).ready(function(){
    $.get('songs.html', function(list){
        var cat = $(list).find('li');
        var dog = [];
        var count = 0;
        var n;
        cat.each(function(i, li){
                dog[count] = $(li).text();
                count = count + 1;
                if(count == 3){
                    n = Math.floor(Math.random()*4);
                    questions.push({"song":"","choice1":dog[0],"choice2":dog[1],"choice3":dog[2],"choice4":dog[3], "answer":dog[n]});
                    count = 0;
                }
        });

        console.log(questions.length);//array has length
    });
    console.log(question.length);

0 个答案:

没有答案