2012-03-22 64 views
8

Có vẻ như "$ smth không phải là chức năng" là một vấn đề rất phổ biến với JavaScript, nhưng sau khi xem qua một vài chủ đề tôi vẫn không thể hiểu nguyên nhân gây ra sự cố .Lỗi JavaScript: "không phải là chức năng"

Tôi có một đối tượng tùy chỉnh, định nghĩa là:

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(); 

Sau đó, trong một kịch bản khác nhau Tôi cố gắng để sử dụng API này theo cách sau:

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

Các getAPI() công cụ, trông giống như sau:

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

Bây giờ, API là có lẽ tìm thấy, bởi vì tôi không nhận được thông báo "Không thể tìm thấy ...", mã sẽ tiếp tục cố gắng khởi tạo nó. Nhưng firebug cho tôi biết API.LMSInitialize is not a function và nếu tôi cố gắng gỡ lỗi bằng alert(Object.getOwnPropertyNames(API));, nó sẽ cho tôi một cảnh báo trống.

Tôi đang thiếu gì?

+1

gì bạn nhận được khi bạn chỉ cần làm một 'console.log (API) 'đúng sau 'API = getAPI();'? – m90

+0

xin vui lòng cho tôi biết những gì bạn muốn làm sau khi bắt đầu .. –

Trả lời

11

Chức năng LMSInitialize của bạn được khai báo bên trong hàm Scorm_API_12. Vì vậy, nó có thể được nhìn thấy chỉ trong phạm vi của Scorm_API_12 chức năng.

Nếu bạn muốn sử dụng chức năng này như API.LMSInitialize(""), kê khai Scorm_API_12 chức năng như thế này:

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

Aha! Đó là những gì sẽ xảy ra khi một bản sao/dán đi sai. Cảm ơn bạn! – SaltyNuts

10

Để biết thêm generic tư vấn về gỡ lỗi loại vấn đề MDN có một bài viết tốt 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.

Về cơ bản đối tượng (tất cả các hàm trong js cũng là đối tượng) không tồn tại ở nơi bạn nghĩ. Điều này có thể cho nhiều lý do bao gồm (không phải là một danh sách đầy đủ):

  • Thiếu thư viện script
  • Typo
  • Chức năng nằm trong một phạm vi mà bạn hiện không có quyền truy cập vào, ví dụ: :

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();

  • Đối tượng của bạn/chức năng không có chức năng gọi điện thoại của bạn:

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

+1

Tôi muốn thêm: đặt tên một biến cục bộ giống như một hàm, vì vậy khi bạn gọi 'showOrderForm()', kiểu 'showOrderForm' là một boolean. – Noumenon

Các vấn đề liên quan