Unexplainable Uncaught TypeError: undefined is not a function

Unexplainable Uncaught TypeError: undefined is not a function

我在 运行 Chrome 上的以下代码时遇到此错误:Uncaught TypeError: undefined is not a function,在线它被写为注释。几天来我一直在互联网上寻找类似的问题,但没有解决我的问题。这里的JS代码:

var myArr = [];
function loadExams() {
    var token = getCookie("token");
    var avgArr = [];
    var avgZero = {
        sAvg: 0,
        sCfu: 0,
        uDate: ""
    };

    dataIn = {
        "action": "loadExams",
        "token": token
    };
    ajaxPost(dataIn, "localPHP.php", function(dataOut) {
            for (var i = 0; i < dataOut.length; i++) {
                $("#editPick").append("<option value=" + dataOut[i]['n'] + ">" + dataOut[i]['sub'] + ": " + dataOut[i]['mark'] + " (" + dataOut[i]['date'] + ")" + "</option>");
                if (i === 0) {
                    console.log(avgArr); //ok
                    console.log(dataOut[0]); //ok
                    console.log(avgZero); //ok
                    avgArr[0] = avg(dataOut[0], avgZero); //HERE ERROR >>>>>>>>>>>>>>> Uncaught TypeError: undefined is not a function <<<<<<<<<<<<<<<<<<<
                } else {
                    avgArr[i] = avg(dataOut[i], avgArr[i - 1]); // same error here!
                }
            }
            for (i = 0; i < myArr.length; i++) {
            avg += myArr[i].mark * myArr[i].cfu;
            cfu += myArr[i].cfu * 1;
        }
        i = dataOut.length - 1;
        var avg = avgArr[i].sAvg / avgArr[i].sCfu;
        document.getElementById('avg').innerHTML = Math.round(avg * 100) / 100;;
    });
}

function avg(cur, prev) {
    var avgTmp = {
        sAvg: prev.sAvg,
        sCfu: prev.sCfu,
            uDate: cur.date
    };
    if (cur.type === "full") {
        avgTmp.sAvg += cur.mark * cur.cfu;
            avgTmp.sCfu += cur.cfu * 1;
    } else {
        for (var j = 0; j < myArr.length; j++) {
            console.log(i + " " + cur.sub + " <> " + j + " " + myArr[j].sub);
            if (myArr[j].sub === cur.sub) {
                avg += (myArr[j].mark + cur.mark) / 2 * cur.cfu;
                cfu += cur.cfu * 1;
                myArr.splice(j, 1);
                var flag = true;
                break;
            }
            if (!flag) {
                j = myArr.length;
                var tmp = {};
                tmp.sub = cur.sub;
                tmp.mark = cur.mark;
                tmp.cfu = cur.cfu;
                myArr[j] = tmp;
            }
        }
    }
    console.log(avgTmp);
    return avgTmp;
}
function ajaxPost(data, php, callback) {
    ax = $.ajax({
        type: "POST",
        data: data,
        url: php,
        success: function (raw_data) {
            f = $.parseJSON(raw_data);
            callback(f);
        },
        error: function (xhr, desc, err) {
            console.log("xhr: " + xhr + "\nAjax error. Details: " + desc + ", Error:" + err);
        }
    });
}

我认为这是因为在函数 loadExams 中有一个语句

 var avg = avgArr[i].sAvg / avgArr[i].sCfu;

这将影响您的函数定义(因为它在更内部的范围内)。将变量重命名为其他名称(或函数)。

作为最佳实践,将变量定义在范围的顶部,这样更容易跟踪。