JS在函数中创建全局变量并在另一个函数中使用它

时间:2018-11-22 10:11:30

标签: javascript

我无法在函数中创建全局变量

function getFabrics(){
    var xhttp = new XMLHttpRequest();
    xhttp.open("GET", "af_general.php?action=fabrics", true);
    xhttp.send();
    xhttp.onreadystatechange = function() {
        if (this.readyState === 4 && this.status === 200) {
            fabrics_json=JSON.parse(this.responseText);
            console.log(fabrics_json);
        }
    };
}

并在另一个中使用它:

function addrow(){
    getFabrics();
    var table=document.getElementById('stoixeia');
    var rows = table.getElementsByTagName("tr").length;
    var row = table.insertRow(-1);
    var cell1 = row.insertCell(-1);
    var rowno=rows-1;
    var pcs_ent=document.createElement('input');
    setAttributes(pcs_ent,{'type':'number','id':'pcs.'+rowno,'name':'pcs','style':'width: 4em;text-align:right','min':'0','max':'99','autocomplete':'off'});
    cell1.innerHTML='#'+rowno;
    cell1.appendChild(pcs_ent);
    var cell2 = row.insertCell(-1);
    var fabric_ent=document.createElement('select');
    console.log(fabrics_json);//returns undefined
    for (f in fabrics_json){
        option = document.createElement('option');
        option.value=fabrics_json[f][0];
        option.text=fabrics_json[f][1];
        fabric_ent.add(option);
    }
    //and goes on...
}

我已经阅读了stackoverflow中所有可能的问题,但找不到找到结果的方法。无论我是否在脚本的开头声明fabrics_json都未定义。

2 个答案:

答案 0 :(得分:1)

JavaScript中的全局变量是window对象的成员。将全局变量称为window.fabrics_json

如果两个函数都在同一个源文件中,则可以在全局范围内声明var fabrics_json;,并使用带和不带window.前缀来访问它。

答案 1 :(得分:0)

我不会在这里使用全局变量...没有必要。您只需要一个简单的回调函数。

//Pass in a callback function..
function getFabrics(callback){
    var xhttp = new XMLHttpRequest();
    xhttp.open("GET", "af_general.php?action=fabrics", true);
    xhttp.send();
    xhttp.onreadystatechange = function() {
        if (this.readyState === 4 && this.status === 200) {
            callback(JSON.parse(this.responseText));
        }
    };
}


function addrow(){
    // pass in your code to getFabrics as a function to be executed on the callback.
    getFabrics(function(fabrics_json){
        var table=document.getElementById('stoixeia');
        var rows = table.getElementsByTagName("tr").length;
        var row = table.insertRow(-1);
        var cell1 = row.insertCell(-1);
        var rowno=rows-1;
        var pcs_ent=document.createElement('input');
        setAttributes(pcs_ent,{'type':'number','id':'pcs.'+rowno,'name':'pcs','style':'width: 4em;text-align:right','min':'0','max':'99','autocomplete':'off'});
        cell1.innerHTML='#'+rowno;
        cell1.appendChild(pcs_ent);
        var cell2 = row.insertCell(-1);
        var fabric_ent=document.createElement('select');
        console.log(fabrics_json);//returns undefined
        for (f in fabrics_json){
            option = document.createElement('option');
            option.value=fabrics_json[f][0];
            option.text=fabrics_json[f][1];
            fabric_ent.add(option);
        }
        //and goes on...
    });
}