2012-03-22 12 views
8

Sembra che "$ smth non è una funzione" è un problema molto comune con JavaScript, ma dopo aver esaminato alcuni thread non riesco ancora a capire cosa lo causa nel mio caso .Errore JavaScript: "non è una funzione"

Ho un oggetto personalizzato, definito come:

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

Poi, in un copione diverso Sto cercando di utilizzare questa API nel seguente modo:

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

Il getAPI) stuff (, assomiglia a questo:

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

Ora, l'API è probabilmente trovato, perché non ottengo il messaggio "Impossibile trovare ...", il codice procede per provare a inizializzarlo. Ma firebug mi dice API.LMSInitialize is not a function e se provo a eseguirne il debug con alert(Object.getOwnPropertyNames(API));, mi dà un avviso vuoto.

Cosa mi manca?

+1

Che cosa si ottiene quando basta fare un 'console.log (API)' destra dopo l'API = getAPI(); '? – m90

+0

per favore puoi farmi sapere cosa vuoi fare dopo l'inizializzazione .. –

risposta

11

La funzione LMSInitialize è dichiarata all'interno della funzione Scorm_API_12. Quindi può essere visto solo nello scope della funzione Scorm_API_12.

Se si desidera utilizzare questa funzione come API.LMSInitialize(""), dichiarare Scorm_API_12 funzione come questa:

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! Questo è quello che succede quando una copia/incolla va male. Grazie! – SaltyNuts

10

Per ulteriori generica consigli su debug questo tipo di problema MDN avere un buon articolo 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.

Fondamentalmente l'oggetto (tutte le funzioni in js sono anche oggetti) non esiste dove pensi che faccia. Questo potrebbe essere per numerosi motivi tra cui (non una lunga lista):

  • mancanti della Libreria di Script
  • Typo
  • La funzione è all'interno di un ambito che al momento non ha accesso a, ad esempio, :

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

  • vostro/la funzione non ha la funzione di vostra vocazione:

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

+1

Vorrei aggiungere: nominare una variabile locale equivale a una funzione, quindi quando chiamate 'showOrderForm()' il tipo di 'showOrderForm' è un booleano. – Noumenon

Problemi correlati