Undefined不是带有"defined"的函数错误.论点

Undefined is not a function error with "defined" argument

本文关键字:quot 函数 错误 论点 defined Undefined      更新时间:2023-09-26

所以这是处理xmlhttprequest的简单JavaScript代码的一部分。生成错误的部分位于底部(else if):

    httpReq.onreadystatechange = function(){
        if (httpReq.readyState == 4) {
            if (httpReq.status == 200) {
                var strRes = httpReq.responseText;
                if(xmlOrig) strRes = (new $Xml(strRes, true)).conteudo(xmlOrig);
                if(elemDest) $id(elemDest).innerHTML = strRes;
                if(func) {
                    var dadosArray = new Array(4, strRes, httpReq, 'OK', 'Concluído com sucesso.');
                    window[func](dadosArray);
                }
            } else {
                if(elemDest) elemDest.innerHTML = 'Erro: '+httpReq.status+' | '+httpReq.statusText;
                if(func) {
                    var dadosArray = new Array(4, false, httpReq, 'erro', 'Erro, conteúdo não carregado!');
                    window[func](dadosArray);
                }
            }
        } else if(func){
            console.log("func? "+typeof(func));
            var dadosArray = new Array(httpReq.readyState);
            window[func](dadosArray);  // <-- HERE IS THE ERROR!
        }
    }

然而,console.log返回"func"参数作为一个函数,那么错误在哪里?

Safari控制台:

函数?函数TypeError: 'undefined'不是函数(对'windowfunc'求值)

你确定func在窗户上吗?您正在检查func,它可以在任何范围内,然后您可以调用window.func() .

您可能打算做window["func"]而不是window[func]

后一个表达式等价于window["function(someParam) { ... }"](即,无论func的实际内容是什么)。window可能没有一个属性,其名称是func的整个字符串化文本内容。