Erreur JavaScript: "n'est pas une fonction"
Ça ressemble à du "$qch n'est pas une fonction" est un problème très commun avec le JavaScript, mais après en regardant à travers quelques fils que je ne peut toujours pas comprendre ce qui en est la cause dans mon cas.
J'ai un objet personnalisé, défini comme:
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();
Puis dans un autre script que je suis en train d'utiliser cette API de la manière suivante:
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;
}
La getAPI() des choses, ressemble à ceci:
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;
}
Maintenant, l'API est probablement trouvé, parce que je n'ai pas l' "Impossible de trouver..." le message, le code produit pour essayer de les initialiser. Mais firebug me dit API.LMSInitialize is not a function
et si j'essaie de le déboguer avec alert(Object.getOwnPropertyNames(API));
il me donne un vide d'alerte.
Ce qui me manque?
source d'informationauteur SaltyNuts
Vous devez vous connecter pour publier un commentaire.
Votre
LMSInitialize
fonction est déclarée à l'intérieur d'Scorm_API_12
fonction. De sorte qu'il peut être vu que dansScorm_API_12
fonction de la portée.Si vous souhaitez utiliser cette fonction comme
API.LMSInitialize("")
déclarerScorm_API_12
fonction comme ceci: