JavaScript错误:"不是函数"

Sal*_*uts 22 javascript javascript-objects

看起来"$ smth不是函数"是一个非常常见的JavaScript问题,但在查看了很多线程后,我仍然无法理解在我的情况下导致它的原因.

我有一个自定义对象,定义为:

function Scorm_API_12() {
var Initialized = false;

function LMSInitialize(param) {
    errorCode = "0";
    if (param == "") {
        if (!Initialized) {
            Initialized = true;
            errorCode = "0";
            return "true";
        } else {
            errorCode = "101";
        }
    } else {
        errorCode = "201";
    }
    return "false";
}

// some more functions, omitted.
}

var API = new Scorm_API_12();
Run Code Online (Sandbox Code Playgroud)

然后在另一个脚本中,我尝试以下列方式使用此API:

var API = null;

function ScormProcessInitialize(){
    var result;

    API = getAPI();

    if (API == null){
        alert("ERROR - Could not establish a connection with the API.");
        return;
    }

    // and here the dreaded error pops up
    result = API.LMSInitialize("");

    // more code, omitted
    initialized = true;
}
Run Code Online (Sandbox Code Playgroud)

getAPI()的东西,如下所示:

var findAPITries = 0;

function findAPI(win)
{
   // Check to see if the window (win) contains the API
   // if the window (win) does not contain the API and
   // the window (win) has a parent window and the parent window
   // is not the same as the window (win)
   while ( (win.API == null) &&
           (win.parent != null) &&
           (win.parent != win) )
   {
      // increment the number of findAPITries
      findAPITries++;

      // Note: 7 is an arbitrary number, but should be more than sufficient
      if (findAPITries > 7)
      {
         alert("Error finding API -- too deeply nested.");
         return null;
      }

      // set the variable that represents the window being
      // being searched to be the parent of the current window
      // then search for the API again
      win = win.parent;
   }
   return win.API;
}

function getAPI()
{
   // start by looking for the API in the current window
   var theAPI = findAPI(window);

   // if the API is null (could not be found in the current window)
   // and the current window has an opener window
   if ( (theAPI == null) &&
        (window.opener != null) &&
        (typeof(window.opener) != "undefined") )
   {
      // try to find the API in the current window?s opener
      theAPI = findAPI(window.opener);
   }
   // if the API has not been found
   if (theAPI == null)
   {
      // Alert the user that the API Adapter could not be found
      alert("Unable to find an API adapter");
   }
   return theAPI;
}
Run Code Online (Sandbox Code Playgroud)

现在,可能找到API ,因为我没有得到"无法找到..."消息,代码继续尝试初始化它.但是firebug告诉我API.LMSInitialize is not a function,如果我尝试调试它alert(Object.getOwnPropertyNames(API));,它会给我一个空白警报.

我错过了什么?

Lia*_*iam 18

有关调试此类问题的更一般的建议,MDN有一篇好文章TypeError:"x"不是函数:

尝试调用类似函数的值,但该值实际上不是函数.有些代码希望您提供一个函数,但这并没有发生.

也许函数名中有拼写错误?也许您调用方法的对象没有此功能?例如,JavaScript对象没有map函数,但JavaScript Array对象没有.

基本上,对象(js中的所有函数也是对象)并不存在于您认为的位置.这可能有很多原因,包括(不是一个广泛的清单):

  • 缺少脚本库
  • 错字
  • 该函数位于您当前无权访问的范围内,例如:

var x = function(){
   var y = function() {
      alert('fired y');
   }
};
    
//the global scope can't access y because it is closed over in x and not exposed
//y is not a function err triggered
x.y();
Run Code Online (Sandbox Code Playgroud)

  • 您的对象/功能没有您的呼叫功能:

var x = function(){
   var y = function() {
      alert('fired y');
   }
};
    
//z is not a function error (as above) triggered
x.z();
Run Code Online (Sandbox Code Playgroud)

  • 我想补充一点:将局部变量命名为与函数相同的东西,所以当你调用 `showOrderForm()` 时,`showOrderForm` 的类型是一个布尔值。 (2认同)

Jus*_*Mad 15

您的LMSInitialize函数在Scorm_API_12函数内声明.所以它只能在Scorm_API_12函数的范围内看到.

如果你想使用这个函数API.LMSInitialize(""),声明这样的Scorm_API_12函数:

function Scorm_API_12() {
var Initialized = false;

this.LMSInitialize = function(param) {
    errorCode = "0";
    if (param == "") {
        if (!Initialized) {
            Initialized = true;
            errorCode = "0";
            return "true";
        } else {
            errorCode = "101";
        }
    } else {
        errorCode = "201";
    }
    return "false";
}

// some more functions, omitted.
}

var API = new Scorm_API_12();
Run Code Online (Sandbox Code Playgroud)


0xa*_*mal 6

我也遇到了这个错误。在我的情况下,根本原因与异步相关(在代码库重构期间):未等待构建“非函数”函数所属的对象的异步函数,随后尝试调用该函数会引发错误,例如以下:

const car = carFactory.getCar();
car.drive() //throws TypeError: drive is not a function
Run Code Online (Sandbox Code Playgroud)

修复是:

const car = await carFactory.getCar();
car.drive()
Run Code Online (Sandbox Code Playgroud)

发布此信息以帮助其他人面临此错误。