2012-03-22 134 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