2012-03-22 127 views
8

它看起来像“$ smth不是一个函数”是JavaScript的一个非常常见的问题,但通过查看相当多的线程后,我仍然无法理解是什么导致它在我的情况。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(); 

然后在不同的脚本,我尝试使用下列方式这个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; 
} 

的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; 
} 

现在,API是可能发现,因为我没有得到“无法找到...”消息,代码继续尝试初始化它。但是萤火虫告诉我API.LMSInitialize is not a function,如果我试着用alert(Object.getOwnPropertyNames(API));进行调试,它会给我一个空白的警报。

我错过了什么?

+1

你明白了什么,当你只是做了'的console.log(API)'权'API = getAPI();'之后? – m90 2012-03-22 15:13:24

+0

请你可以让我知道你想做什么后,启动.. – 2014-01-29 10:01:40

回答

11

您的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(); 
+3

啊哈!这是复制/粘贴出错时发生的情况。谢谢! – SaltyNuts 2012-03-22 15:37:35

10

更多通用建议在调试此类问题MDN有很好的文章TypeError: "x" is not a function

It was attempted to call a value like a function, but the value is not actually a function. Some code expects you to provide a function, but that didn't happen.

Maybe there is a typo in the function name? Maybe the object you are calling the method on does not have this function? For example, JavaScript objects have no map function, but JavaScript Array object do.

基本上,对象(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();

  • 你的对象/功能不具备的功能您的电话:

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

+1

我想补充一点:将一个局部变量命名为一个函数,所以当你调用showOrderForm()时,'showOrderForm'的类型是一个布尔值。 – Noumenon 2017-08-01 13:45:09